Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC and Web API Projects in the same Solution

I created a solution a while ago that contains a Web API 2 project (provides JSON data to mobile devices) and a Class Library (includes my data access services).

The Web API project uses Ninject for DI and everything works fine.

Now I need to add a separate MVC project for a few web pages. The api should be accessible from www.example.com/api/controller, while the website should be accessed through www.example.com/controller.

The problem is that each of these two, has a different "Register" method with seemingly incompatible route collections. If I set the MVC project as the startup project, routes for the api are not registered, and vice versa. If I set "Mutiple startup projects", they run on different ports which is not my cup of tea.

How I can set the MVC project as the startup project, while registering all routes for both of them?

One more thing. Because the Web API project was created sooner, Ninject configuration has been written inside it. Of course, some of the services from the Class Library project are needed inside the new MVC project. Do I have to move Ninject configuration to the MVC project, or they just work because they are run on startup of the Web API Project?

like image 546
Delphi.Boy Avatar asked Apr 01 '15 05:04

Delphi.Boy


People also ask

How to deploy MVC and WebAPI in same project?

This 2 projects are independent from each other like 2 different applications even if they are in the same solution. 1) Deploy your MVC project to www.example.com (main virtual application ). 2) Deploy your WebAPI project to www.example.com/api ( api folder is a virtual application ).

How to create a web API using Visual Studio Code?

Select New Project -> Visual C# -> Web -> ASP.NET Web Application and enter your application and solution name. Select the empty template from options and check Web API checkbox and click OK. The solution will be created as below.

How do I add a web API 2 controller?

In order to add a Web API Controller you will need to Right Click the Controllers folder in the Solution Explorer and click on Add and then Controller. Now from the Add Scaffold window, choose the Web API 2 Controller – Empty option as shown below.

What is web API and why Web API?

Why Web API? Without Web API, the server will have one application to handle the XML request and another application to handle the JSON request, i.e., for each request type, the server will have one application. But with Web API, the server can respond to any request type using a single application.


2 Answers

This 2 projects are independent from each other like 2 different applications even if they are in the same solution.

To succeed what you try to achieve you have to:

1) Deploy your MVC project to www.example.com (main virtual application).

2) Deploy your WebAPI project to www.example.com/api (api folder is a virtual application). Don't forget to remove api from your WebAPI routes (other wise you have to use this route www.example.com/api/api/controller).

Doing this you can acces independently both projects in the same url.

For NInject part, you can register again the services in the MVC project. Another solution will (which i recommend) be to make a class library project and register there the services after that you reference this class library in both projects.

like image 126
adricadar Avatar answered Sep 25 '22 05:09

adricadar


What I like to do, is to have the MVC and WebAPI project in two separate projects (separation of concerns) but let them share the same business logic.

I like to utilize the Command and Query pattern. So all commands and queries is located in another solution project, which both the MVC and WebAPI project has access to.

I deploy the MVC project on the www.domain.com path, the WebAPI project on the api.domain.com and enable CORS on WebAPI for the www origin.

like image 43
janhartmann Avatar answered Sep 21 '22 05:09

janhartmann