eWorld.UI - Matt Hawley

Ramblings of Matt

ASP.NET MVC - Legacy Url Routing

April 25, 2008 00:47 by matthaw

Recently, we've been converting over a lot of our ASP.NET Web Form pages to use ASP.NET MVC. While this is no small feat by itself, the underlying problem of having a new Url structure in the site while still supporting legacy Url's was necessary. The idea, is that you hit a page that no longer exists, and you get redirected to the appropriate controller & action within MVC.

 

Workflow

image

  1. A legacy Url is requested from your site. For example, http://www.server.com/Users/Login.aspx
  2. ASP.NET routing intercepts the request and matches a route from your route collection
  3. Instead of using the MvcRouteHandler, a LegacyRouteHandler is invoked.
  4. Using the LegacyRouteHandler, it'll use the route redirection name you specified, generate the MVC Url, and issue a HTTP 301 with the location of http://www.server.com/site/login.

Routing

First, we should define our legacy route class. This is necessary because we need to expose an additional property to enable our routing handler to find the correct MVC route.

   1: // The legacy route class that exposes a RedirectActionName
   2: public class LegacyRoute : Route {
   3:     public LegacyRoute(string url, string redirectActionName, IRouteHandler routeHandler)
   4:         : base(url, routeHandler)
   5:     {
   6:         RedirectActionName = redirectActionName;
   7:     }
   8:  
   9:     public string RedirectActionName { get; set; }
  10: }

Secondly, we need to define the route handler and associated http handler. The route handler derives from IRouteHandler, and will be the class used when creating your legacy routing. The http handler derives from MvcHandler because it gives us some critical information, like RequestContext. You'll also notice that (while not in the code) you need to copy all of the querystring parameters from the request over. This is a necessary step because the GetVirtualPath method call will take all route data (from RouteData.Values) and try and utilize that when building the Url itself.

   1: // The legacy route handler, used for getting the HttpHandler for the request
   2: public class LegacyRouteHandler : IRouteHandler {
   3:     public IHttpHandler GetHttpHandler(RequestContext requestContext) {
   4:         return new LegacyHandler(requestContext)
   5:     }
   6: }
   7:  
   8: // The legacy HttpHandler that handles the request
   9: public class LegacyHandler : MvcHandler {
  10:     public LegacyHandler(RequestContext requestContext) : base(requestContext) { }
  11:  
  12:     protected override void ProcessRequest(HttpContextBase httpContext) {
  13:         string redirectActionName = ((LegacyRoute)RequestContext.RouteData.Route).RedirectActionName;
  14:  
  15:         // ... copy all of the querystring parameters and put them within RouteContext.RouteData.Values
  16:  
  17:         VirtualPathData data = RouteTable.Routes.GetVirtualPath(RouteContext, redirectActionName, RouteContext.RouteData.Values);
  18:  
  19:         httpContext.Status = "301 Moved Permanently";
  20:         httpContext.AppendHeader("Location", data.VirtualPath);
  21:     }
  22: }

Lastly, you need to create your routes within the Global.asax file. Remember, that order is necessary when setting up routing.

   1: public void RegisterRoutes(RouteCollection routes) {
   2:     routes.MapRoute("Login", "site/login", new {
   3:         controller = "Users",
   4:         action = "DisplayLogin"
   5:     });
   6:  
   7:     routes.Add("", new LegacyRoute(
   8:         "Users/Login.aspx",
   9:         "Login",
  10:         new LegacyRouteHandler()));
  11: }

And that's it. When a request comes in, you'll see the following in Fiddler

  1. A request on "Users/Login.aspx"
  2. A HTTP 301, with a header "Location" and value of "site/login"
  3. A request on "site/login"

Final Thoughts

Granted, there's more you can do with this - like creating your own extension methods like MapRoute and doing better handling of finding the route, but this should get you started. Also, I'm writing the code off the top of my head, so there's no guarantee that any of it works as-is. Please let me know if you have any other thoughts.

Lastly, for those wondering why are we using a HTTP 301 status code? Well read up on them. "301 Moved Permanently" indicates "that all future requests should be directed to the given URI." While your end users will not see any difference other than a new URL in the browser, the 301 status code more aimed towards search engines to update their URL's in their indexes.

kick it on DotNetKicks.com

Currently rated 5.0 by 3 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

April 26. 2008 04:54

Pingback from weblogs.asp.net

ASP.NET MVC - Legacy Url Routing - eWorld.UI - Matt Hawley

weblogs.asp.net

April 29. 2008 15:22

Pingback from code-inside.de

Wöchentliche Rundablage: ASP.NET MVC, Silverlight 2, jQuery, CSS, C#… | Code-Inside Blog

code-inside.de

May 8. 2008 09:59

Pingback from hsidev.wordpress.com

ASP.NET MVC Resources « HSI Developer Blog

hsidev.wordpress.com

October 2. 2008 13:27

Pingback from ytechie.com

ASP.NET MVC, What about SEO?

ytechie.com

October 11. 2008 01:22

Pingback from feeds.feedburner.com

Scott Hanselman's Computer Zen - ASP.NET MVC and the new IIS7 Rewrite Module

feeds.feedburner.com

October 11. 2008 01:23

Pingback from hanselman.com

Scott Hanselman's Computer Zen - ASP.NET MVC and the new IIS7 Rewrite Module

hanselman.com

December 19. 2008 10:32

Here is my scenario. For the example lets say that I need to return a list of cars based on a search criteria. I would like to have a single View to display the results since the output will be the same, but I need several ways of getting there. For instance, I may have a Form with a textbox to search by year. I may have another separate page that contains a hyperlink for all red, Toyota cars. How do I handle these multiple scenarios in the same View and Controller. My dilemma is that the search could contain several options… year, make, model, etc but I don’t know where to put them.

What is the best approach for this? Should I define the parameters in the routing or go with query strings, etc?

Eric Brown

February 14. 2009 06:03

Pingback from msdnbangladesh.net

ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking - Shahed

msdnbangladesh.net

February 14. 2009 06:05

Pingback from msmvps.com

ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking - Shahed Khan (MVP C#)

msmvps.com

February 14. 2009 06:05

Pingback from msbdusers.net

Shahed Khan (MVP C#) : ASP.NET MVC tips: Routing Engine to aid SEO / 301 Redirect / Tracking

msbdusers.net

March 18. 2009 21:29

Wow, I never knew that ASP.NET MVC - Legacy Url Routing. That’s pretty interesting...

David Ascot

March 21. 2009 02:17

This is great.. 1 question though could i use this for requests like http://www.server.com/Users/Login.html . So with file extensions html. Cause i have couple of really old links pointing to a site of mine

Bayram Çelik

April 21. 2009 23:04

good nights.

ankara lazer

May 13. 2009 21:06

Great article. .NET URL Routing has saved us from the URL rewriting. URL writing has always been a painful task when managing our listings in the Search engines such as Google, Live and Yahoo. Thanks!

Sandeep Aparajit

May 20. 2009 14:10

.NET routing sounds interesting i will try this out in my new website

Green Tea Diet Pills

May 25. 2009 14:24

Wow, I never knew that ASP.NET MVC - Legacy Url Routing. That’s pretty interesting..

Web Design Pakistan

May 29. 2009 03:12

thanks nice info...

sunny

June 2. 2009 14:15

Very helpful. Thanks

Chris

June 12. 2009 13:09

That’s great, I never thought about ASP.NET MVC - Legacy Url Routing like that before.

Internet Marketing Company

June 18. 2009 12:07

Nice article.The code works fine and is a good starting point for me making a custom module.Thanks for the module...

SEO

June 22. 2009 23:32

Pingback from dbones.co.uk

A Little more on the MVC Routing

dbones.co.uk

June 24. 2009 07:24

fine and is a good starting point for me making a custom module.Thanks for the module...

degree in sociology

June 24. 2009 07:24

That’s great, I never thought about ASP.NET MVC - Legacy Url Routing like that before.

online economics degrees

June 24. 2009 07:24



That’s great, I never thought about ASP.NET MVC - Legacy Url Routing like that before.

online legal studies

June 24. 2009 07:24

Legacy Url Routing like that before.

online science degree

June 24. 2009 07:25

thanks for the post

health science degree online

June 24. 2009 08:36


Search engines optimisation, SEO, is an evolving 'science' and it keeps changing on purpose. Most articles that
I read which involve both SEO and ASP.NET usually focus on how to programatically set the meta keywords tag and
they tend to make it look like very important.

SEO

June 27. 2009 03:53

Its always good to learn tips like you share for blog posting. As I just started posting comments for blog and facing problem of lots of rejections. I think your suggestion would be helpful for me. I will let you know if its work for me too.
Thanks and keep post such a informative blogs.

texas hold em poker download online

July 1. 2009 06:14

For the example lets say that I need to return a list of cars based on a search criteria. I would like to have a single View to display the results since the output will be the same, but I need several ways of getting there. For instance, I may have a Form with a textbox to search by year.

club penguin

July 2. 2009 09:28

I had to hire a programmer to change the urls of .net based site. I am trying to learn it but it seems a little complicated.

Affordable crm solutions

July 3. 2009 04:04

Hi,

Nice article.....I m very much interested in ASP.Net , i m planning to take this course.......
<a href="http://www.r4-ds-karte.de/">r4 kartes </a>

r4 kartes 

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

July 3. 2009 21:23



Copyright © 2000 - 2009 , Excentrics World