ASP.NET MVC中的URL重定向

Yas*_*ser 4 asp.net webforms asp.net-mvc-routing asp.net-mvc-4

我正在使用之前使用ASP.NET Web Forms构建的网站,现在使用ASP.NET MVC构建.

我们上周制作了新的MVC版本.

但是旧的登录网址www.website.com/login.aspx已被许多用户收藏,他们仍然使用它,因此他们得到404错误.

所以我想知道哪个是将用户从旧网址重定向到新的mvc网址的最简单最好的方法,即www.website.com/account/login

就像这个登录网址一样,我希望用户可能还有其他几个网址的书签,那么处理这个问题的最佳方法是什么?

Dar*_*rov 6

您可以URL Rewrite module在IIS中使用它.就像在您的<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>
Run Code Online (Sandbox Code Playgroud)

该模块非常强大,允许您进行任何类型的重写和重定向.这是其他一些sample rules.


1Ma*_*yur 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");
        }
    }
Run Code Online (Sandbox Code Playgroud)