September 23, 2010

Redirect old aspx to new MVC

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!

3 Comments

Sign in to join the conversation

Sign in
Erin
Is there any reason why you didnt do: if (Request.Path.Contains("/viewdvd.aspx")) { Response.Redirect("/Media/Detail/" + Request.QueryString["upc"]); }
Link
steven
Well that seems entirely too logical and elegant. Kudos.
Link
bc
Nice solution. Response.Redirect is not a permanent redirection (301 status code) it is a 302 (temporary)
Link