URL Redirection in ASP.NET MVC

你说的曾经没有我的故事 提交于 2019-12-06 04:31:10

问题


I was working on a Website which was earlier built with ASP.NET Web Forms and now is built with ASP.NET MVC.

We made the new MVC version live last week.

But the old login url which is www.website.com/login.aspx has been bookmarked by many users and they still use that and hence they get 404 errors.

So I was wondering which would be the easiest and best way to redirect the user from the old url to the new mvc url which is www.website.com/account/login

Like this login url, I am expecting the users may have bookmarked few other urls also, so what will be the best way to handle this ?


回答1:


You could use the URL Rewrite module in IIS. It's as simple as putting the following rule in your <system.webServer> section:

<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>

The module is very powerful and allows you any kind of rewrites and redirects. Here are some other sample rules.




回答2:


in the 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");
        }
    }


来源:https://stackoverflow.com/questions/15737810/url-redirection-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!