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.Cu开发者_如何学编程rrent;
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:
- try to arrive at page:
~/SignIn/Default.aspx
- the requests gets intercepted and fixed to:
~/SignIn/
- fill the form, click sign in
- the current page url goes from:
~/SignIn/
to~/SignIn/Default.aspx
and gets fixed again, thus voiding the processing of the methodSignIn
(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
.
精彩评论