ShaneKm
ShaneKm

Reputation: 21328

MVC4 WebAPI vs. regular controllers

I'm just starting to read up on the new MVC4 WebAPI. This question may be pretty basic but it will resolve my confusion.

Suppose you want to create an MVC site that would also follow REST architecture.

Following this tutorial, when creating a page that would display a list of products would I need to create two controllers => one for regular website (that would derive from "Controller") and another for serving requests via API that would derive from "API controller"?. This seems like a lot of redundancy.

Would you then have only 1 view (website)? and another that would only return (for example some JSON result - WebAPI) ?

EDIT:

what is the standard way of creating ViewModels?. I assume the same view model can be used for both controllers. correct?

Upvotes: 2

Views: 1036

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038770

Following this tutorial, when creating a page that would display a list of products would I need to create two controllers => one for regular website (that would derive from "Controller") and another for serving requests via API that would derive from "API controller"?. This seems like a lot of redundancy.

Yes, you need 2 controllers. It might be redundant but that's how it is at the moment. Hopefully Microsoft will merge the 2 development approaches in future versions. Currently you could also return Razor views from a Web API controller but it could be a little painful to do everything through an API controller.

Upvotes: 2

Related Questions