MVC and web API projects in one solution

I recently created a solution that contains a Web API 2 project (provides JSON data for mobile devices) and a class library (including 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 several web pages. Api should be available from www.example.com/api/controller , and the website should be accessed through www.example.com/controller .

The problem is that each of these two has a different β€œCheck-in” method with seemingly incompatible route collections. If I install the MVC project as a launch project, routes for api are not registered and vice versa. If I install Mutiple startup projects, they will work on different ports that are not my cup of tea.

How can I set the MVC project as a startup when registering all routes for them?

One more thing. Since the Web API project was created earlier, the Ninject configuration was written to it. Of course, some of the services from the class library project are needed in the new MVC project. Do I need to move the Ninject configuration to the MVC project or do they just work because they start when the web API project starts?

+5
source share
2 answers

These 2 projects are independent of each other, like 2 different applications, even if they are in the same solution .

To succeed, you must:

1) Deploy the MVC project at www.example.com (main virtual application).

2) Expand the WebAPI project in the folder www.example.com/api ( api is a virtual application). Remember to remove the api from the WebAPI routes (otherwise you must use this route www.example.com/api/api/controller ).

By doing this, you can independently use both projects in the same URL .

For the NInject part, you can re-register services in the MVC project. Another solution (I recommend) is to make a class library project and register services there after you reference this class library in both projects.

+10
source

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

I like to use the Command and Query pattern. Thus, all the commands and requests are in another solution project, to which both MVC and WebAPI projects have access.

I am deploying the MVC project along the path www.domain.com , the WebAPI project on api.domain.com and api.domain.com CORS into the WebAPI for www origin.

+1
source

Source: https://habr.com/ru/post/1216584/


All Articles