从 url 中剥离 default.aspx 和 //www
剥离 /Default.aspx
和 //www
的代码无法正常工作(如预期):
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);
}
}
它通常可以工作,但是在提交表单(例如登录)时上面的代码拦截请求,然后重定向到同一页面。 示例:
- 尝试到达页面:
~/SignIn/Default.aspx
- 请求被拦截并修复为:
~/SignIn/
- 填写表单中,单击
- 当前页面 url 中的登录从:
~/SignIn/
到~/SignIn/Default.aspx
并再次修复,从而使该方法的处理无效SignIn
(这会将浏览器重定向到/SignIn/Success/
),并且页面将重新加载为~/SignIn/
并且没有登录完成了。
请帮忙。不知道这里要解决什么/如何解决。
这里的主要要求是:
从网址中删除 /Default.aspx
和 //www
thnx
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:
- 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题与 GET 和 POST 请求有关。当您调用 Response.Redirect 时,您指示客户端向您提供的 URL 发出新的 GET 请求。因此,如果您在请求(例如实际上是 POST 请求的表单回发)中尽早调用此函数,则会丢失该帖子。由于大多数 POST 应该在操作完成后自行重定向,因此仅将上面的逻辑应用于 GET 请求可能就足够了。
您可以使用
Request.HttpMethod
访问请求方法(GET 或 POST)。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
.