views:

538

answers:

2

Hi,

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

+6  A: 

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.

Brad Wilson
+4  A: 

You would actually define multiple routes... the defined actions in your controller would go first with the default being at the bottom. I like to think of route definitions as a "big 'ole switch statement" where first rule satisfied wins..

routes.MapRoute(
               "ArtistPostComment",
               "artists/PostComment/{id}",
               new { controller = "Artists", action = "PostComment", id = "" }
);
routes.MapRoute(
               "ArtistDeleteComment",
               "artists/DeleteComment/{id}",
               new { controller = "Artists", action = "DeleteComment", id = "" }
);
routes.MapRoute(
               "ArtistDefault",
               "artists/{artistName}",
               new { controller = "Artists", action = "Show", artistName = "" }
);
datacop