views:

36

answers:

1

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

+1  A: 

Your problem here is to do with GET and POST requests. When you call Response.Redirect, you instruct the client to make a new GET request to the URL you provide. So if you call this early in a request like a form postback that was actually a POST request, you lose the post. Since most POSTs should themselves redirect once the action is complete, it may be enough to just only apply the logic you have above to a GET request.

You can access the request method (GET or POST) using Request.HttpMethod.

David M
indeed. thank you. i checked for context.Request.HttpMethod.ToString().Equals("GET") and returned if not so. it is **HttpMethod** though, not Method (for full correctness if anyone uses this).
b0x0rz