Tuesday, August 24, 2010

How to append RootUrl to ReturnUrl parameter in login page

We can append Root Url to return url parameter in the login page and do a response redirect to same page so that return url value will get updated to routed value.
Example:
public partial class Login : Page
{
    public string RootUrl
    {
        get
        {
            Uri requestUri = Context.Request.Url;
            HttpRequest request = Context.Request;
            string rootUrl = string.Format("{0}{1}{2}{3}{4}",
                requestUri.Scheme,
                Uri.SchemeDelimiter,
                requestUri.Host,
                requestUri.IsDefaultPort ? string.Empty : string.Format(":{0}", requestUri.Port),
                request.ApplicationPath);
            return rootUrl.EndsWith("/") ? rootUrl : string.Format("{0}/", rootUrl);
        }
    }
 
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        string returnUrl = this.Request.QueryString.Get("ReturnUrl");
        if (!string.IsNullOrEmpty(returnUrl))
        {
            if (!returnUrl.ToLower().Contains("http://"))
            {
                returnUrl = Path.Combine(this.RootUrl, returnUrl);
                string url = string.Format("{0}?ReturnUrl={1}", this.Request.Path, Server.UrlEncode(returnUrl));
                this.Response.Redirect(url);
            }
        }
    }
}
Referene

No comments:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...