views:

302

answers:

1

I have a legacy asp.net website I am migrating to asp.net mvc.

I would like to permanently redirect existing urls to the asp.net mvc controllers. I have controllers with views for the new location for these links, and I would like to do a 301 redirect on the existing pages to the new views.

I have two different types of urls:

  1. http://foosite.com/privacy.aspx
  2. http://foosite/bar/content-name

Type 2 urls are the result of an existing url rewriter module from before asp.net mvc route handling.

I have existing redirect code:

Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", url);
Response.End();

Where should I do the redirect?

I see two options:

Application_BeginRequest - use regex to parse the url

What I like about it:

  • I already check for uppercase urls to redirect to lowercase urls here
  • I have the chance to work directly with the response without having to return an ActionResult

What I don't like about it:

  • Url type #2 from the top has to be mapped into the new controllers/views, meaning I need to do some database work to get the path for the url

Controller Actions - use the routes & controllers to do the redirect

What I like about it:

  • I can cleanly do the database work I need

What I don't like about it:

  • The controller needs to return a view, and I am directly manipulating the Response stream to create the 301.

Any suggestions would be great, thanks.

+3  A: 

Among those two choices, I would go with Controller actions. Controllers aren't required to return a view--I believe you can even make a controller method return void with ASP.NET MVC. The reason I like this option is because of the database interaction--I think spinning up a database in the BeginRequest is going to affect overall performance.

If that's not a concern, I think putting it with the rest of the routing information makes the most sense (i.e. with BeginRequest).

mgroves
I have been leaning towards the controllers as well. Not calling the service layer in global.asax is what sold me. The weird part is messing with the Response directly and returning a null view.
blu
Did you look into returning a RedirectToAction or something like that?
mgroves
I don't really want to redirect to an action right? I want to send the http response for 301 and let the browser handle the redirect.
blu
Can you post some samples on your suggestions? I have a similar issue I am working on http://stackoverflow.com/questions/1320041/what-is-a-fairly-easy-to-implement-url-rewriter-for-asp-net-mvc
Picflight