Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Using the MVC Route engine to preserve URL Routes for SEO

Today I was making some changes to the DimeCasts.Net site and I realized that I did not like the URL's that I was currently using as I did not feel they accuracy reflected the intent of the route.  However, because Google has already linked many of the episodes on the site, I did not want to break those links.  At the same time I did not want to leave obsolete code laying around in my controllers. 

So what was I to do?  Simple, use the Routing engine to my advantage.

Below you find both the old URL and the new URL, notice the subtle change

Old: ../Casts/CastItemsByLevel/Advanced
New: ../Casts/CastsByLevel/Advanced

Basically all you need to do is the following

  1. Rename your old controller action to your new controller action name.
    Hint: If you are using a tool like ReSharper or CodeRush, they may be helpful here.
  2. Duplicate (copy/paste) your old route (assuming you have created a new route).
    1. Change the name of the route.  I added a _obsolete
    2. Leave the URL parameter the same
    3. Modify the parameter defaults to reflect your new controller action
      new { controller = "Casts", action = "NewActionNameHere" }
  3. Modify your new route to be the correct route for the new URL you desire
  4. Update your view name if you desire, or you will have to specify that in your RenderView call
  5. Test/Ensure that the old route goes to your new controller action
  6. Test/Ensure that your new route goes to your new controller action
  7. Sit back and be satisfied that you have not broken any external links

Here are my routes, both old and new:

// old route
routes.MapRoute(
    "CastsByLevelName_obsolete",                                         // Route name
    "Casts/CastItemsByLevel/{levelName}",               // URL with parameters
    new { controller = "Casts", action = "CastsByLevel" } // Parameter defaults
);

// new route
routes.MapRoute(
    "CastsByLevelName",                                         // Route name
    "Casts/CastsByLevel/{levelName}",               // URL with parameters
    new { controller = "Casts", action = "CastsByLevel" } // Parameter defaults
);

Hope this helps someone.

Till next time,

[----- Remember to check out DimeCasts.Net -----]



Comments

Dew Drop – June 4, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop – June 4, 2008 | Alvin Ashcraft's Morning Dew

# June 4, 2008 8:40 AM

Christopher Steen said:

Link Listing - June 4, 2008

# June 5, 2008 6:39 AM

Christopher Steen said:

MSBuild DevSource Article: Automation with MSBuild [Via: Steve ] WPF The Missing .NET #4: Cue Banner...

# June 5, 2008 6:39 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News