[RESOLVED]How to work with Web API and MVC together in same project

i search google but not found a details article on How to work with Web API and MVC together in same project.  basically people host web api in separate project but i like to know how could i have web api controller in same mvc project. i want to know how
mvc controller can communicate with web api controller. looking for details discussion with sample code & details instruction. thanks

below article gives you step by step implementation of ASP.NET Web API in MVC application

http://dotnetmentors.com/mvc/web-api-in-mvc-tutorial.aspx

mou_inn

How to work with Web API and MVC together in same project.

A WebAPI class by default can be placed at any folder in the MVC project. Sometimes I create an API sub folder under Controller folder and keep all my WebAPI controllers.

mou_inn

i want to know how mvc controller can communicate with web api controller

WebAPI is typically called from an Ajax request, i.e, from the browser (though not necessarily). If MVC and WebAPI reside in the project, then there is no point calling a WebAPI from a MVC controller

suppose i need to call web api action from mvc action method then what will be the code……guide me. thanks

Hi mon,

Thanks for your post.

WebApi is an alternative Service oriented application from Microsoft just like WCF. But WCF uses SOAP protocol and WebAPI uses HTTP protocol for communication.

So if you are using WCF to provide service for your MVC application you would host that wcf service seperately and consume its service by MVC application, EXACTLY same way you have to host your WebAPI project seperately and provide service to your Web application
(MVC).

for some reasons if you want them (MVC and WebAPI) to use in the same project, follow this rules from this article.

http://odetocode.com/blogs/scott/archive/2013/07/01/on-the-coexistence-of-asp-net-mvc-and-webapi.aspx

More information:Add Web API to an existing MVC 5 Web Application

Hope this can be helpful.

Best Regards,

Eileen

Hi,

Go through the links I posted. It shows you exactly how to do that.

regards,

Yorrick

mou_inn

suppose i need to call web api action from mvc action method then what will be the code

Typically you need to call a Web API that is remotely hosted from MVC. In that scenario please take a look at this post

Calling a Web API From a .NET Client

Leave a Reply