In your MVC sites, you often need to serve up miscellaneous static content, such as http://example.com/About
, http://example.com/Contact
, etc.
Instead of creating controllers for each of these, you can organize them into a single controller like so:
routes.MapRoute(
"StaticContent",
"{action}",
new { controller = "StaticContent" },
new { action = "About|Contact" } // Add more here
);
Create your StaticContentController
with action methods About()
and Contact()
, and you're good to go!
The important part is the constraint new { action = "About|Contact" }
(which is a regular expression) that will prevent any other URLs from inadvertently using this route.