0

I am developing an ASP.NET MVC application, that has a lot of client side interaction ie. a lot of ajax calls to controller actions.

Currently I have controllers containing actions (part of the same controller) returning both views and data (json/xml), I am considering is it a good practice to have actions returning data and views under one controller.

For example under

/Controllers/CarsController/
public ActionView Index()
public ActionView Car(int carID)
public ActionResult CarMakes() 
public ActionResult CarModels(int makeID)



/Controllers/StoresController
public ActionView Index()
public ActionView Store(int storeID)
public ActionResult Cities()
public ActionResult Stores()

This is one way of thinking, and the other would be

/Controllers/CarsController/
public ActionView Index()
public ActionView Car(int carID)

/Controllers/StoresController
public ActionView Index()
public ActionView Store(int storeID)

/Controllers/ServicesController
public ActionResult CarMakes() 
public ActionResult CarModels(int makeID)
public ActionResult Cities()
public ActionResult Stores()

The second way offers an easier way to find all the available actions that could be used for client side interaction. Actions avoids duplication, could also be reused and called from different controllers (they can be called and reused in either way)

So between these two I would just like to know what is general practice when designing MVC application?

  • Would maybe depend a bit on what type of data exactly thos extra controllers like CarMakes(), Cities() and Stores() return. What for example is the difference between StoresController/Index and StoresController/Stores? So some of those could even be just additional params to the Index() action like if CarModels returns a list of cars as Index. Or you could use nested routes like in a RESTful design where CarMakes is one independent resource I guess (then there should be a CarMakesController with index action. But the CarModels would be like /carmake/#car_make_id/carmodels. – thorsten müller Oct 6 '15 at 8:10
  • 1
    Just dropping everything in a over generalized services controller seems the worst idea. – thorsten müller Oct 6 '15 at 8:10
  • Possibly you should have more controllers. If cities in fact returns a list of cities and not a list of shops you should handle it as a resource and it should have its own Controller, most likely the same for CarModels (at least if stored independently from Cars) and similar CarMakes. – thorsten müller Oct 6 '15 at 8:16
  • @thorstenmüller regarding your first answer, all actions returning ActionView return a View. Overrides could apply for actions accepting different params. All actions returning ActionResults return plain data json or xml. – John Oct 6 '15 at 9:35
1

The first approach seems better. Keep all actions related to a certain object (usually a model) under a single controller. You will know for sure that if you need to change an existing action related to cars, it will be located under CarsController. This makes your code easier to maintain.

It's perfectly normal for a single controller to contain different types of actions.

  • 1
    It's perfectly normal for a single controller to contain many different types of actions. would maybe depend on what exactly you define as 'many'. Much more than your common index + crud is mostly a bad design. You may add one or two additional actions in special cases but even that should be the exception. – thorsten müller Oct 6 '15 at 8:14
  • @thorstenmüller true. I meant it as "more than one type". I edited the answer to clarify. – Hugo Zink Oct 6 '15 at 8:16
  • @thorstenmüller you mentioned that having more then one action differed by CRUD is mostly a bed design. Basically you are saying that for my first example I would need 8 different controllers CarController, CarFinderController, CarMakeController, CarModelController, StoreController, CityController, StoreFinderController. You could be right, but I did not understand that controllers should be used in that way. Is there a MVC reference or explanation which actually explains this pattern? – John Oct 6 '15 at 9:44
  • @thorstenmüller I belive that controller is a class, and controller should reflect a good class design....depending on its needs. – John Oct 6 '15 at 9:48
  • 1
    @John, this would be resources in RESTful designs. Googling REST would give you a lot of information. In part such architecture decisions can be community dependent. In Ruby on Rails it's near mandatory, while I'm not sure about asp.net. Still worth looking. (Also CarFinder may be just an additional action in car or even a specific variation of the index action). – thorsten müller Oct 6 '15 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.