22

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 project within the same solution which sits along side the MVC project, again atop the N-tier application.

But I am unclear as to how this all works as the MVC layer is the startup project: it sets up my DI, automapper, etc. and is the project I deploy to the server.

So how should this all be set up? Can I set up my MVC project to route all /api requests to the new Web API project? Or does the Web API project need to be deployed separately?

I don't want to be doing anything unconventional so if there is a much more common way of setting this up, please point me in the right direction.

Thanks.

CarenRose
  • 1,266
  • 1
  • 12
  • 24
James
  • 1,979
  • 5
  • 24
  • 52
  • 2
    Why has this been edited? What have you improved over my original posting? Infact, all you have done is highlight various parts which dont need highlighting! – James Sep 03 '13 at 10:03

3 Answers3

12

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

billybob
  • 2,859
  • 6
  • 35
  • 55
Alagesan Palani
  • 1,984
  • 4
  • 28
  • 53
  • So the N-Tier application should actually be wrapped in the Web Api which the MVC presentation project consumes? Wouldn't this cause the MVC project to be slow, given it would need to call a web service for its data? – James Sep 03 '13 at 10:10
  • Also, would you then move the MVC project into a separate solution altogether? – James Sep 03 '13 at 10:17
  • 2
    Yes, you have to create/develop your webapi project and MVC project seperately and host them seperate, Its good design and highly scalable. – Alagesan Palani Sep 03 '13 at 10:37
  • 1
    I would say create two solutions, one for MVC web application with all your sub projects related to web application , another for web api (service application) and its related project like.. getting data from db or something and do processing and expose them to web application as a service. Now, host them seperately on IIS. – Alagesan Palani Sep 03 '13 at 10:47
  • It certainly sounds like a good approach as the MVC project then becomes just another consumer of the service. But does it slow down the responsiveness of the MVC web site, with it having to wait for data to be returned across a network from the Web Api? – James Sep 03 '13 at 10:52
  • there can be negligible delay as you are exposing your service and client both in the same network or in same place geographically. – Alagesan Palani Sep 03 '13 at 10:59
  • @AlagesanPalani What about the startup issue? Should we also select WebAPI project as Startup project besides the MVC project? – Jack May 19 '15 at 17:10
  • @Christof You want to select both the WebAPI and MVC projects to startup. They will run as separate applications, much like they will when they are hosted separately on IIS. – sfinks_29 Jun 16 '15 at 02:28
  • @sfinks_29 Thanks for your answer. – Jack Jun 16 '15 at 07:06
8

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.

Hugo Hilário
  • 2,848
  • 2
  • 27
  • 43
0

You just need to add the routing and API controllers to your existing MVC site where they will all be available under site/api/

Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60
  • I have considered putting both into one project but this seems to be over complicating the project. It would be great to keep the 2 things separate. – James Sep 03 '13 at 10:00