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
- 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. - Duplicate (copy/paste) your old route (assuming you have created a new route).
- Change the name of the route. I added a _obsolete
- Leave the URL parameter the same
- Modify the parameter defaults to reflect your new controller action
new { controller = "Casts", action = "NewActionNameHere" }
- Modify your new route to be the correct route for the new URL you desire
- Update your view name if you desire, or you will have to specify that in your RenderView call
- Test/Ensure that the old route goes to your new controller action
- Test/Ensure that your new route goes to your new controller action
- 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 -----]
Posted
06-03-2008 7:29 AM
by
Derik Whittaker