Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Redirection in ASP.NET MVC

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 ?

like image 262
Yasser Shaikh Avatar asked Dec 21 '22 07:12

Yasser Shaikh


2 Answers

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.

like image 117
Darin Dimitrov Avatar answered Jan 02 '23 09:01

Darin Dimitrov


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");
        }
    }
like image 21
1Mayur Avatar answered Jan 02 '23 10:01

1Mayur