301重定向在a​​sp.net 4.0 [英] 301 redirect in asp.net 4.0

查看:95
本文介绍了301重定向在a​​sp.net 4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现URL重定向的网站,而不是网页做网页。我想这样做在Global.asax文件。下面是code我已经定义了。

我想有 http://website.net 作为我的主要网址&放大器;想有一个永久的URL,如果有人类型的 http://www.website.net 重定向。

不幸的是它不工作现场网站。任何人都可以指出问题在code。在code不产生任何错误。

 无效的Application_Start(对象发件人,EventArgs的发送)
{
    //上的应用程序启动时运行code    如果(HttpContext.Current.Request.Url.ToString()。ToLower将()。包含(http://website.net))
    {
        HttpContext.Current.Response.Status =301永久移动;
        HttpContext.Current.Response.AddHeader(位置,R​​equest.Url.ToString()ToLower将()更换(http://website.net,http://www.website.net));
    }}


解决方案

主要问题:Your're做上述东西在的Application_Start - 这是只执行一次。你应该挂钩每个请求。试试这个:

 无效的Application_BeginRequest(对象发件人,EventArgs的发送)
{
    //即在每次请求运行code    如果(HttpContext.Current.Request.Url.ToString()。ToLower将()。包含(http://website.net))
    {
        HttpContext.Current.Response.Status =301永久移动;
        HttpContext.Current.Response.AddHeader(位置,R​​equest.Url.ToString()ToLower将()更换(http://website.net,http://www.website.net));
    }}

这是更好的方法是使用URL重写,这可以从的Web.Config 中配置:

<一个href=\"http://stackoverflow.com/questions/10153670/microsoft-rewriting-module-force-www-on-url-or-remove-www-from-url\">Microsoft重写模块 - 强制WWW上的URL或删除网址 WWW

I am trying to implement URL redirect for the website rather than doing it page by page. I want to do it in the global.asax file. Below is the code i have defined.

I want to have http://website.net as my main url & want to have a permanent URL redirect if someone types in http://www.website.net.

Unfortunately it is not working for the live website. Can anyone point out the problem in the code. The code doesn't generate any error.

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup

    if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.net"))
    {
        HttpContext.Current.Response.Status = "301 Moved Permanently";
        HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.net", "http://www.website.net"));
    }

}

解决方案

Main problem: Your're doing the above stuff in Application_Start - which is only executed once. You should hook up with each request. Try this:

void Application_BeginRequest(object sender, EventArgs e) 
{
    // Code that runs on every request

    if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.net"))
    {
        HttpContext.Current.Response.Status = "301 Moved Permanently";
        HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.net", "http://www.website.net"));
    }

}

An even better approach would be to use URL rewriting, which can be configured from within Web.Config:

Microsoft rewriting module - Force www on url Or remove www from url

这篇关于301重定向在a​​sp.net 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆