Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC & Web Api projects within same Solution

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.

like image 314
James Avatar asked Sep 03 '13 09:09

James


People also ask

What is MVC?

MVC (Model-View-Controller) is a pattern in software design commonly used to implement user interfaces, data, and controlling logic. It emphasizes a separation between the software's business logic and display.

Is MVC a framework?

MVC is one of the most frequently used industry-standard web development framework to create scalable and extensible projects.

What is MVC known for?

MVC is known as an architectural pattern, which embodies three parts Model, View and Controller, or to be more exact it divides the application into three logical parts: the model part, the view and the controller.

Is MVC a OOP?

MVC is a software design pattern built around the interconnection of three main component types: Model, View, and Controller, often with a strong focus on Object-Oriented Programming (OOP) software paradigms. MVC is a framework for building web applications using an MVC Design.


2 Answers

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

like image 126
Alagesan Palani Avatar answered Sep 27 '22 23:09

Alagesan Palani


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.

like image 43
Hugo Hilário Avatar answered Sep 27 '22 22:09

Hugo Hilário