简体   繁体   中英

stripping default.aspx and //www from the url

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM