How do you handle your routing in your web application? Do you use all the time the default routing rule?
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Or do you write your own rules for custom functionality when you want something different for each webpage? There is no problem to use the default routing rule and its siblings but there are times when you end up doing repetitive works that you could avoid easily.
Most of the time it is all good to use simple routing rules, but what do you do when you have to create a lot of similar pages, with the same views, or when you need different views but with the same kind of data? What if the only thing you need is static content pages but you don’t want the .html ending in your url and/or you want to use Razor. Do you still create a new route or controller for each of these pages?
If you do it can be a lot of time to maintain them. Either your RouteConfig.cs can grow to a size where it is hard to find anything. Or you end up creating a lot of empty and unnecessary Controller with a lot of copy pasted actions. Or better, you have to copy and paste the same views all time when you need a new page and if you want to keep your pages consistent you have to edit each of these pages one by one after each change request. As these pages grow, the time to maintain them grows equally. But why would you do that when you could modify all of your pages from one place and keep the unique content in separate places. So when a shared design changes it will be reflected across all of the pages and you had to modify it in one place. If you need a new page with the same logic, you just have to create the custom content and forget the complexity of new routing, controllers and growing maintenance.
These are frequent cases and unfortunately I rarely see elegant solutions for these problems. In the upcoming posts I will examine all of these situations and I will show you how I ended up solving them. I hope it will ease your daily maintenance nightmare.
Let’s examine different situations that could cause problems and where you could save tremendous amount of time developing your application further and also maintaining it.
PLEASE NOTE
For this post and its sub-posts I created a demo git project that serves as a working example for each of these situations. So you can see these examples in context and you can try them out yourself. Please note, as I extend this post with new examples that project will grow too.
Git: https://github.com/botondev/ASP.NET-MVC5-CustomRoute
I will use this post as a home page of a series of posts that will discuss one single situation. So you can come back any time to see if there is any new update.
Situations And Examples
Static page with Razor (One route, Single controller action, Multiple Views)
Instead of multiple custom routes and multiple controller actions you can have only one route and one controller action to handle all of your static pages with Razor and still make sure to continue the MapRoute lookup if there is no existing static page defined by the url.
Product details page with mixed unique static and dynamic data
Problem: Same logic in each controller action (with different lookup). Your design team wants to write a custom description for each product in static HTML but the main part of the page is the same across products.
You could use one controller action with unified logic, using the url parts to feed strategies for custom content (db lookup and partial_views).