ASP.NET MVC 3 – Managing Strategy with Actions and Controllers

asp.net-mvc-3mvc

Can anyone name any usefull strategy/architectural pattern for allocating actions between different controllers when using MVC pattern for developing web application?

I am now developing web app using asp.net Mvc3 framework and still can't figure out how to manage actions and controllers. One approach is to create single action controller for each url, but it's not the best choice since to much controllers have to be created. Should I list all available urls that are supported by me web app, devide them into groups and create separate controller for each group or act in any different manner?

It seems like I will become face to face with some kind of mess with no consistent approach in managing actions and controllers.

Best Answer

The best thing for you to do is just to start with the default MVC scheme - where controllers and actions are 'allocated' by URL convention, with the first part of the path determining the controller, and the rest of the URL (and the request type, e.g. GET or POST) determining the action that'll be called.

Whilst at first this seems like it must lead to a 'mess' the reality is that most websites don't actually require loads of controllers, and certainly don't require loads of actions.

Sometimes you might wish to do some clever stuff to split one controller into two, whilst keeping the URLs the same - e.g you might have:

~/Products going to ProductsController.GetProducts()

~/Products/{id} going to ProductsController.GetProduct(id)

But then, let's say you have reviews of products too, you might have

~/Products/{id}/Reviews going to ProductReviewsController.GetReviews(id)

This could be legitimate since reviews functionality might require quite a lot of code, which isn't directly related to the products themselves.

That said, the motivation for splitting controllers shouldn't be to keep a code-file small (you can do that with partial classes), it should be to keep to a single-responsibility principle; that is - a controller should have responsiblity for single section of your site. The Users controller should not also be used for your homepage.

Don't spawn a new controller for everything you do, consider it based on the entity that the controller will operate on or provide details of (e.g. products, users etc). Thinking URL-first and then using that as the basis for new controllers is a very good starting point, because ultimately you want your site to be easily navigable by search engines as well as users.

The beauty of MVC, also, is that with the separation of controllers from the route patterns that trigger their actions, you can completely change the URL structure of all your existing code just by changing the routing. For that reason it's particularly important to remember always to use the UrlHelper for generating URLs to other actions, and HtmlHelper's ActionLink and RouteLink operations in views for generaing cross-links; these honour routing, and so take away a lot of the pain associated with re-structuring a website at the URL level.

Related Topic