mostlylucid

STATIC ARCHIVE of mostlylucid.co.uk of old
posts - 892, comments - 676, trackbacks - 11

My Links

News

follow me on twitter!

Archives

Post Categories

Misc. Coding

Work in progress, Response.RelativeRedirect

Inspired by this post, only covers simple cases but it's a start. Essentially this is an extended version of Response which only allows redirection to pages within the same site...so allows /default.aspx, does not allow http://www.evildomain.com/default.aspx. I've also ripped off a member of my team's excellent work on Response.Redirecting to a new window. This method uses extension methods, to use it just drop the file in App_Code and Response gets two new members. Oh and it's incomplete because I didn't account for encoded / obfuscated URLs...I'll update when I do...

 

using System;

using System.Web;

using System.Web.UI;

 

public static class EnhancedRedirect

{

    public static void RelativeRedirect(this HttpResponse response, string path)

    {

        RelativeRedirect(response, path, "_self");

    }

 

    public static void RelativeRedirect(this HttpResponse response, string path, string target)

    {

        RelativeRedirect(response, path, target, string.Empty);

 

    }

 

    public static void RelativeRedirect(this HttpResponse response, string path, string target, string windowFeatures)

    {

        if (!string.IsNullOrEmpty(path) && !path.StartsWith("http:") && !path.Contains("//"))

            Redirect(response, path, target, windowFeatures);

        else

        {

            throw new InvalidOperationException("Cannot redirect outside of the current site.");

        }

    }

 

    public static void Redirect(this HttpResponse response, string url, string target, string windowFeatures)

    {

        Redirect(response, path, target, string.Empty);

    }

 

    public static void Redirect(this HttpResponse response, string url, string target, string windowFeatures)

    {

        if ((String.IsNullOrEmpty(target) || target.Equals("_self", StringComparison.OrdinalIgnoreCase)) && String.IsNullOrEmpty(windowFeatures))

        {

            response.Redirect(url);

        }

        else

        {

            Page page = (Page)HttpContext.Current.Handler;

            if (page == null)

            {

                throw new InvalidOperationException("Cannot redirect to new window outside Page context.");

            }

            url = page.ResolveClientUrl(url);

            string script;

            if (!String.IsNullOrEmpty(windowFeatures))

            {

                script = @"window.open(""{0}"", ""{1}"", ""{2}"");";

            }

            else

            {

                script = @"window.open(""{0}"", ""{1}"");";

            }

            script = String.Format(script, url, target, windowFeatures);

            ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true);

        }

    }

}

Print | posted on Wednesday, April 23, 2008 3:00 PM | Filed Under [ ASP.NET Code Snippets ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 1 and type the answer here:

Powered by: