Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working on an application that will have both web and mobile interfaces. That's why I want to use Web API. I don't have any problem with the Web API part, but I don't know what to do with the MVC application.

As far as I understand, my data access layer and Entity Framework will be in Web API, but I still have to create my Model classes in MVC for things like data formatting and..., data validation will take place in Web API, and I'll use Ajax calls to Web API from my Views in MVC to access data. Now, the question is, what's the role of Controller in MVC then?

Can someone tell me what design pattern should be used to develop an asp.net MVC application that consumes data from an asp.net Web API application?

share|improve this question
    
Instead of using MVC, you could use a client-side framework, since that would provide you with a rich application interface for both web and mobile. You could leverage responsive design and actually build one app for both platforms. Check out Google's Angular, or Knockout, either would tie in nicely with Web API. –  Chris Hardie Feb 6 at 21:13
    
My questions is a conceptual one. So, is it correct to say that MVC and Web API do not fit each other, and in fact you use MVVM with Web API? –  ataravati Feb 6 at 21:33
1  
I do use Web API with my MVC apps, but I rarely use the HttpClient to consume them. I almost always use JavaScript to consume them from the client. If you are going to do a lot of UI updating from data from Web API, I would recommend a MVVM framework, which would replace MVC. –  Chris Hardie Feb 6 at 21:45
    
This question is not too broad, unless you don't understand it. There are some similar questions this this here on SO. –  ataravati Feb 6 at 23:39
add comment

closed as too broad by Robert Harvey Feb 6 at 22:45

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 3 down vote accepted

If you want to leverage the ASP.NET MVC framework you could centralize business logic and data access in service classes that would be used by both your Web API and MVC application. I usually avoid calling Web API from MVC when I own both applications.

On the other hand, if you want to consume your API through ajax requests, you should use a client-side JavaScript framework such as AngularJS. In this scenario, you barely use the MVC framework. Your MVC application will serve the initial view, perform login/logout and optionally bundle and minify js and css files.

Of course, you can chose a strategy somewhere in between. For example, you might not want to dive into the complexity of a single page web application and use jQuery for ajax requests instead. In this case, you might need to use your MVC application as a proxy to your services or Web API to avoid cross-domain issues.

In short, there is no single best design pattern. Here are some factors that could influence your design : budget, complexity, user experience, responsiveness, browsers and targeted users. For instance, it is unnecessary to use cutting-edge client-side technologies for 2-3 users using IE6 to fill endless forms with data.

share|improve this answer
    
Thank you! This and Chris Hardie's comments answered my question. –  ataravati Feb 6 at 23:43
add comment

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