Asp.net 从域到www.domain的全局301重定向
我可以使用Global.asax的begin请求重定向所有内容吗 从mydomain.domain到www.mydomain.domainAsp.net 从域到www.domain的全局301重定向,asp.net,vb.net,http-status-code-301,global-asax,Asp.net,Vb.net,Http Status Code 301,Global Asax,我可以使用Global.asax的begin请求重定向所有内容吗 从mydomain.domain到www.mydomain.domain 如果这是真的,我该怎么做呢?对简的答案做了一些小改动,让我明白了这一点: protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e) { string currentUrl = HttpContext.Current.Request.Path.ToLowe
如果这是真的,我该怎么做呢?对简的答案做了一些小改动,让我明白了这一点:
protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
string currentUrl = HttpContext.Current.Request.Path.ToLower();
if(currentUrl.StartsWith("http://mydomain"))
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", currentUrl.Replace("http://mydomain", "http://www.mydomain"));
Response.End();
}
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string currentUrl = HttpContext.Current.Request.Url.ToString().ToLower();
if (currentUrl.StartsWith("http://mydomain"))
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", currentUrl.Replace("http://mydomain", "http://www.mydomain"));
Response.End();
}
}
更改是使用BeginRequest事件,并将currentUrl设置为HttpContext.Current.Request.Url,而不是HttpContext.Current.Request.Path。见:
您好,我发现global.asax中不存在PreRequest处理程序,所以我按照您的建议添加了它。但事件不会在调试模式下激发。。。我还做错了什么?当您将
PreRequestHandlerExecute
更改为BeginRequest
时,它是否会触发?是的!BeginRequest会在每个请求中激发,然后将内部部分移动到BeginRequest,这样就可以了。Request.Path只返回虚拟路径,不返回域名。上面的代码不起作用。BeginRequest发生在PreRequestHandlerExecute之前还是之后?