Yasser Shaikh
Yasser Shaikh

Reputation: 47804

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 ?

Upvotes: 4

Views: 4408

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039428

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.

Upvotes: 6

1Mayur
1Mayur

Reputation: 3485

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

Upvotes: 5

Related Questions