I upgraded Dvdcrate.com to be ASP.NET MVC and one of the problem is the old pages are indexed by search engines and being served on search results. So I have the old *.aspx page that I need to fix up and send along to the MVC page.
An example of the page in the search database;
www.dvdcrate.com/viewdvd.aspx?upc=5027035006161
And I need it to go to the new MVC controller and action like this;
www.dvdcrate.com/Media/Detail/5027035006161
So I did it this way, I put this in the Global.aspx.cs file;
protected void Application_BeginRequest (object sender, EventArgs e) { var requestUrl = Request.Url.ToString().ToLower(); if (requestUrl.Contains("/viewdvd.aspx")) { var equalPos = requestUrl.LastIndexOf("=") + 1; var upc= requestUrl.Substring(equalPos, requestUrl.Length - equalPos); Context.Response.StatusCode = 301; Context.Response.Redirect("/Media/Detail/" + upc); } }
Probably not the most elegant, but it does work.
Enjoy!