Is there any way to use ASP.NET CORE MVC and WEB API together? - asp.net-core

I am new to ASP.NET CORE and interested in how MVC and WEB API could be combined together? For example is there any way to use WEB API for back-end and MVC for front-end? The application should be simple CRUD app.

Easiest way is to create an MVC application.
Just create an api-controller inside and you have both!
A web api is just MVC, without the V, that is a model and a controller, no view.

Related

Can I have asp.net core mvc and web api as single project

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? 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 :-
Certainly you can. What all you need is, you should have one solution. Furthermore, within the solution, you ought to create two project. One for MVC application and another for Web API application. Here is the simulation how you can achieve that:
Project Architecture Would be Like:
If you want to run two project together:
Go to your solution and right click on it
Select Property
Choose Multiple Startup Project

Can we write the .net core WebAPI in the asp.net core razor web page itself?

I have the following projects in my solution all .net core 5.0.
myproject.domain
myproject.infrastructre
myproject.webAPI
myproject.UI.Razorweb page.
Instead of using SPA which calls the webAPI, I am using razor web page which will call the webAPI(.net 5.0). My question is can we merge the webAPI and the razorwebpage into one? Can we write webAPI in the razor web page itself?
Is that allowed?
I agree with the suggestion given by Jeremy.
You should have a web API and Razor project separate.
Besides, if you would like to make some tests then you could refer to the articles below.
Use Razor Pages, MVC, And Web API In A Single ASP.NET Core Application
Hosting ASP.NET Core Razor Pages and Web APIs in a Single Project
Razor pages and webapi in the same project

Web Application vs Web Api project types in Asp.net Core

I know that Asp.Net MVC and Asp.Net Web API were merged into one code in Asp.net Core and they inherit from Controller base class and can all return implementations of IActionResult. it be a View for MVC or Json for web api.
But when i want to create a Asp.net Core project, it offers two templates (Web Application and Web Api ), according to what i said in above, there is no differences between these controllers, why there is two templates? is there any differences that i don't know about it?
The web application template will create folders and import stuff needed for a web application such as jquery, css etc. Web api template will create folders and import stuff for a web api. Also the controllers created by default will have different implementations, for example, web application will be returning views and the views will be created in the appropriate folder.
So although they derive from the same controllers, each type of project requires different dependencies.
If I were you I would go ahead and create one for each type and see the difference.
If you want to have both web api and web application in the same project, use areas. This way your web and api will have separate controllers, folders and models. Also if you want to separate them in the future, it will be easy to do so.
The difference between 2 templates is-
The WebAPI template starts with a Controller class that will allow you to respond to RESTful requests at the /api/Values endpoint.
The Web Application template will give you an MVC framework enabled project with some Razor views, the bootstrap CSS framework and jQuery library installed.
If you want to create project with both MVC and API controllers then I would suggest to go with ASP.NET Core Web Application template and add require dependencies.

Controller life-cycle ASP.NET Core WebApi

I'm using asp.net core to develop a WebApi.
What is the life-cycle of controller in ASP.NET Core? Does it depends on the web server?
Usually an instance of a controller is created per request and if you use dependency injection, all associated services are created for that request.

Introducing WebAPI in ASP.NET MVC4 application

I am working on an MVC4 application that uses MVC controllers and it is already in production. But a new requirement is to expose some of the functionality as a web service so it is accessible to get/post data programatically by other applications. So I am thinking of introuding WebAPI in this application and use it to provide an iterface to external world as well as use the same service by the views in my application.
My question is how can I use WebAPI in my existing controllers so that I can return Views instead of raw Json data.
Thanks