I have an MVC 4 project sitting atop an N-tier application. I now have a requirement to to be able to consume the application programmatically. I have created a new Web API proj
I just did the same thing yesterday. I have in the same MVC 4 project regular Controllers and ApiControllers.
You need to add the routing in the Global Asax for WebApi :
WebApiConfig.Register(GlobalConfiguration.Configuration);
Take a look at the WebApiConfig :
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Don't forget also to add the Nuget Packages for WebApi (if you don't have them already). In my case I did not had them because my project was originally MVC 3 and was later upgraded.
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
You just need to add the routing and API controllers to your existing MVC site where they will all be available under site/api/