MVC4 WebAPI versus regular controllers

I am just starting to read the new MVC4 WebAPI. This question may be quite simple, but it will resolve my confusion.

Let's say you want to create an MVC site that also follows the REST architecture.

Following this , when creating a page that will display a list of products, I need to create two controllers => one for the normal website (which will be output from the "Controller") and the other to serve the API requests that will be received from " API controller "?. It looks like redundancy.

Will you only have 1 view (website)? and another one that will only return (e.g. some JSON result - WebAPI)?

EDIT:

What is the standard way to create ViewModels ?. I am guessing that the same view model can be used for both controllers. right?

+3


source to share


1 answer


Following this tutorial, when creating a page displaying a product listing, I would have to create two controllers => one for the regular website (which will be retrieved from the "Controller") and the other for serving API requests that will be retrieved from the "API- controller "?. It looks like redundancy.



Yes, you need 2 controllers. It might be overkill, but like now. Hopefully, in future releases, Microsoft will combine the two development approaches. Currently, you can also return Razor views from a web API controller, but it would be a little painful to do everything through an API controller.

+2


source







All Articles