A set of technologies in .NET for building web applications and web services. Miscellaneous topics that do not fit into specific categories.
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