stripping default.aspx and //www from the url

Posted by b0x0rz on Stack Overflow See other posts from Stack Overflow or by b0x0rz
Published on 2010-05-31T16:58:15Z Indexed on 2010/05/31 17:03 UTC
Read the original article Hit count: 297

Filed under:
|
|
|

the code to strip /Default.aspx and //www is not working (as expected):

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            string url = context.Request.RawUrl.ToString();

            bool doRedirect = false;

            // remove > default.aspx
            if (url.EndsWith("/default.aspx", StringComparison.OrdinalIgnoreCase))
            {
                url = url.Substring(0, url.Length - 12);
                doRedirect = true;
            }

            // remove > www
            if (url.Contains("//www"))
            {
                url = url.Replace("//www", "//");
                doRedirect = true;
            }

            // redirect if necessary
            if (doRedirect)
            {
                context.Response.Redirect(url);
            }
        }

it works usually, but when submitting a form (sign in for example) the code above INTERCEPTS the request and then does a redirect to the same page. example:

  1. try to arrive at page: ~/SignIn/Default.aspx
  2. the requests gets intercepted and fixed to: ~/SignIn/
  3. fill the form, click sign in
  4. the current page url goes from: ~/SignIn/ to ~/SignIn/Default.aspx and gets fixed again, thus voiding the processing of the method SignIn (which would have redirected the browser to /SignIn/Success/) and the page is reloaded as ~/SignIn/ and no sign in was done.

please help. not sure what / how to fix here.

the main REQUIREMENT here is:

remove /Default.aspx and //www from url's

thnx

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET