Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC routing action name conflicts

We have a website that deals with artists and venues and we're developing it in ASP.net MVC.

We have our artist views in a folder (Views/Artists/..), an ArtistsController, ArtistsRepository and adhere to the REST action names such as Show, New, Delete etc.

When we first mocked up the site, everything worked well in our test environment as our test URLs were /artists/Show/1209 but we need to change this so the website appears as /artists/Madonna and /artists/Foo-Fighters etc

However, how can we distinguish between valid artist names and the names of the actions for that controller?! For example, artists/PostComment or artists/DeleteComment? I need to allow the routing to handle this. Our default Show route is:

routes.MapRoute(
               "ArtistDefault",
               "artists/{artistName}",
               new { controller = "Artists", action = "Show", artistName = ""}

One way around this is for our website to visibly run on /artists, but have our controller renamed to the singular - ArtistController - as opposed to ArtistsController. That would go against the naming conventions we went with when we started (but hey!).

Do you have any other recommendations? If possible we could also route depending on the verbs (so PostComment would be a POST so we could perhaps route to that action), but I'm not sure if that is advisable let alone possible.

Thanks

like image 873
Perhentian Avatar asked Dec 09 '22 21:12

Perhentian


1 Answers

The 4th parameter to MapRoute allows you to specify restrictions for values. You can add a route before this one that is for "artists/{action}/{id}" with a restriction on the valid values for action; failing to match one of your actions, it'll fall through to the next route which will match for artist name.

like image 70
Brad Wilson Avatar answered Dec 29 '22 20:12

Brad Wilson