ASP.NET MVC routing: clean addresses - from camel case to portable words

I currently have an action defined in my controller:

// GET: /schools/:cleanUrlName/data-loggers public ActionResult DataLoggers(string cleanUrlName) { return View(); } 

This works when I click "/ school / brisbane-state-high-school / dataloggers", however - according to the comment - I want to access it via a slightly cleaner URL (using hyphens): "/ school / brisbane - state-high school / data loggers. " I know that I can write a route to accomplish this, but I was hoping that I would not have to write a new route for each multitask action / controller. Is there a better way to solve this problem?

+1
asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing
Sep 03 '13 at 5:11
source share
1 answer

You can use ActionNameAttribute to create an alias for your action name.

So, you just need to annotate your reusable actions:

 [ActionName("data-loggers")] public ActionResult DataLoggers(string cleanUrlName) { return View("DataLoggers"); } 

But since it also affects view detection, you need to return View("DataLoggers") so that you are probably better off creating custom routes for your reusable actions.

+2
Sep 03 '13 at 5:24
source share



All Articles