Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does web API and MVC work together?

How does MVC and web API work together in an application?

Are you supposed to use javascript binding (with knockout.js and ajax calls) and just use MVC for the actual container page?

Or do you call web API from the MVC controller (like a WCF service layer)?

The integration of MVC and web API just isn't clear to me.

Should I use Web API if I regulary require HTML to be returned (i.e. I want to work with Partials)?

I'd like to use web API so I can scale my app though (return HTML from one side, but remain with an API that can return/process XML)!

Thanks for clearing it up :)

like image 895
Team-JoKi Avatar asked May 09 '26 15:05

Team-JoKi


2 Answers

This picture below from the link explains clearly how MVC and Web Api work together:

enter image description here

Technically, you can call from MVC to Web API, but it is not really the best practice since:

  1. Calling from MVC to Web API will across the network (distribution), this makes your application more complex.

  2. Web Api is REST Api, it is not like WCF which is heavily SOAP Api (although WCF support REST Api). So, from JavaScript you can call the Web Api easier using ajax.

like image 163
cuongle Avatar answered May 11 '26 05:05

cuongle


Here's how I set up my latest MVC and Web API application: There's the regular model layer (*M*VC) and I have created a service layer for the business rules. The controllers of my MVC application call the service layer to process data from and to the views.

My Web API application is a external interface to the methods on the service layer, therefore both the controllers on the MVC application and on the Web API application call the service layer. I hope this helps.

like image 45
Anderson Fortaleza Avatar answered May 11 '26 05:05

Anderson Fortaleza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!