So can I have MVC and Web API inside the same project?
Yes, you can simply add the controller for the ASP.NET Core Web API in the existing Controllers folder. You do not have to add the Web API project.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
We want to build an asp.net core web application. it contains MVC part for returning Views and API to return JSON and have Restful services for our web application.
So can I have MVC and Web API inside the same project? as currently when i want to create a new project inside .NET 6.0 i have to select the either MVC or Web API type :-
So can I have MVC and Web API inside the same project?
Yes, you can simply add the controller for the ASP.NET Core Web API in the existing Controllers folder. You do not have to add the Web API project.
Hi @john john ,
can I have MVC and Web API inside the same project? as currently when i want to create a new project inside .NET 6.0 i have to select the either MVC or Web API type
Yes, you can add them in the same project.
The easiest way is to use the "ASP.NET Core Web App (Model-View-Controller)" template to create the project, then you can add the API controller in the Controllers folder.
Right click the Controllers folder, select Add => Controller option, then in the popup window, select the API controller:
By using this method, in the program.cs file, it already configured the MVC routing, and for the API controller, it will use the [Route]
attribute to configure the endpoint.
If you are using the "ASP.NET Core Web API" template to create the project. In the project, it only contains the Controllers folder, and the in the Program.cs file it only configure the routing for the Controller, without the view.
So, in this scenario, you have to add the Models and Views folder, add the Layout page and so on (refer to the new MVC application) by yourself. And in the Program.cs file, you have to configure the MVC route.
So, I suggest you can use the "ASP.NET Core Web App (Model-View-Controller)" template to create the project, and then add the API controller in the Controller folder.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion