2013-04-01 75 views
4

我正在使用ASP.NET Web Forms构建的网站,现在使用ASP.NET MVC构建网站。ASP.NET MVC中的URL重定向

上周我们做了新的MVC版本。

但旧登录URL是www.website.com/login.aspx得到了众多用户的书签,他们仍然使用,因此他们得到404错误。

所以我想这将是从旧的URL将用户重定向到新的MVC网址的最简单,最好的办法是www.website.com/account/login

筛选登录网址,我期待用户也可能已经收藏了其他几个网址,那么处理这个问题的最佳方式是什么?

+0

为什么不保留旧页面并从中重定向。网页表单和MVC路线可以在同一个项目中存在... –

+0

已完全移除旧项目 – Yasser

+0

您可以在global.asax文件中检查URL ...如果URL是www.website.com/login.aspx那么您可以将用户重定向到www.website.com/account/login –

回答

5

在Global.asax

void Application_BeginRequest(Object source, EventArgs e) 
    { 
     //HttpApplication app = (HttpApplication)source; 
     //HttpContext context = app.Context; 

     string reqURL = HttpContext.Current.Request.Url; 

     if(String.compare(reqURL, "www.website.com/login.aspx")==0) 
     { 
      Response.Redirect("www.website.com/account/login"); 
     } 
    } 
+1

你可以实现其他逻辑来匹配的网址以及 – 1Mayur

+1

真棒我用它做301需要的重定向。 – Spaceman

6

你可以在IIS中使用URL Rewrite module。这就像把下面的规则在你<system.webServer>节一样简单:

<system.webServer> 
    <rewrite> 
     <rules> 
      <rule name="Login page redirect" stopProcessing="true"> 
       <match url="login.aspx" /> 
       <action type="Redirect" redirectType="Permanent" url="account/login" /> 
      </rule> 
     </rules> 
    </rewrite> 

    ... 
</system.webServer> 

该模块是非常强大的,让你任何种类的重写和重定向。这里有一些其他sample rules

+0

看起来不错..... – 1Mayur