asp.net mvc legacy route mapping

asp.net-mvc, c#, url-rewriting

Solution

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

Problem

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: - http://foosite.com/privacy.aspx - 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.

Original source