Lecture thumbnail 0:00 / 9:29 In ASP.NET Core, Minimal API is the new way to create the Web API or HTTP services.
If you wonder, what is the meaning of HTTP or Web API services?
See, there can be some kind of client applications such as
React or Angular or any single-page applications or mobile applications.
The problem with these mobile applications or single-page applications is
it requires the back-end services.
Means, they depend on the services such as ASP.NET Core applications
to store or interact with the data, to manage the user accounts,
to fetch or retrieve or update or store the information.
So, there is a need of creating HTTP or Web API services.
In ASP.NET Core, we have Web API controllers.
So, you can create the HTTP services by using Web API controllers.
So, you can create the HTTP services that can receive the request from the single-page applications
and can process the request and provide the response back to the single-page applications.
For example, if you want to create something like a banking project,
there can be a specialized team to create the single-page application for the banking project.
Or the banking solution.
And there must be another team on ASP.NET Core who work on creating HTTP services.
So, those services or the Web API controllers receive the request from the single-page application.
For example, they can receive the GET, POST, PUT and DELETE requests
and can perform the CRUD operations on the appropriate database tables.
So, the traditional way to create such HTTP or Web API services is Web API controllers.
In this case, you will create a regular controller in ASP.NET Core,
but you will decorate the same with an attribute called API controller.
But the current topic that we are trying to talk about is the alternative way for the same.
So, the Microsoft is currently working on developing the minimal API that is the alternative way to create HTTP services.
Instead of using Web API controllers, the developers can use minimal API to create HTTP services.
So, what makes the difference between all these?
In this course, we have fundamentally learned MVC controllers.
That means your controller class inherits from Microsoft.aspnetcore.mvc.controller base class,
and it has out-of-the-box support for model binding, model validations, views, filters, and filter pipeline.
So, if you want to work with the views directly,
means if your controller action method returns the view result,
then choosing MVC controller is an appropriate one.
But in case if you don’t want to create MVC views,
but alternatively if you want to create Angular or React kind of applications as views,
then there is a need of creating HTTP services by using ASP.NET Core,
and that is called Web API services or HTTP services.
The traditional way to solve this problem is,
you will create a controller and decorate the same with API controller attribute,
so it can act as HTTP service.
It can receive the GET, POST, PUT, DELETE requests.
It has full support for model binding and model validations, much like MVC controllers does.
But the difference is, it doesn’t support the views.
Because in this case, the scope of the ASP.NET Core application is limited to
only controllers and models, but not the views.
Because instead of you create MVC views,
you are going to create Angular or React to replace the same.
Now, with the version ASP.NET Core 6 onwards,
Microsoft introduces Minimal API as alternative to the traditional Web API services,
but currently it is under still active development and continuous improvements,
so as of this exact moment, it may not be the perfect choice for creating
production-strength applications for creating HTTP services,
but hopefully within 2 to 3 years,
the Minimal API will be stable and usable in the production-strength code to create HTTP services.
But if you compare the features between the API controllers and Minimal API,
the Minimal API has limited support for custom model binding and custom model validators.
So if you plan to write your own custom model binding or custom model validators,
much like you can do with the regular controllers,
it has limited support for the same, so the exact same code doesn’t work with the Minimal API.
Of course, it supports model binding, it can receive the model object,
it can transform the request details into a model object.
For example, it can read the values from the query string
and assign the same values into a model object.
It is possible, but it may not have the full-fledged support for custom model binding.
And expectedly, the Minimal API doesn’t support the views.
See, if you want to create the MVC views,
you should not opt either of these API controller or Minimal API.
You should be using MVC controllers like we have done so far from the beginning of the course,
up to these 25 sections.
The common goal of both API controllers and Minimal API is
to serve the data as HTTP requests that can be accessible from
the applications such as Angular application or React application
or any other mobile applications such as Android or iOS applications.
So in this context, those single-phase applications such as Angular or React applications
or mobile applications act as a view or the UI layer,
whereas our ASP.NET Core application developed with API controller or Minimal API
will be backend for them.
And moreover, if we talk about filters,
the Minimal API doesn’t support all types of filters
that are supported by the controllers of MVC.
So it doesn’t support authentication filters, action filters, etc.
But it has support for only one type of filters, that is endpoint filters.
They work almost equivalent to action filters.
They execute before logic as well as after logic for every endpoint.
So in order to learn your Minimal API,
you should go back to the beginning of the course
and recollect what we have done exactly in the sections of routing,
middleware, and model binding,
and then forget about all the things that we have done after learning the razor views.
So to learn this Minimal API, keep these concepts in mind,
that is routing, middleware, model binding, accept custom model binding,
and dependency injection.
That’s it.
With this knowledge, you can proceed to learn Minimal API.
Do you remember these methods to create endpoints?
I mean, app.mapget and app.maphost?
These methods create endpoints to receive the request at a particular route
and executes the particular piece of the code
as soon as the request is reached to the particular route.
For example, for this route, if you make a get request,
the first method executes,
but if you make a post request, the second method executes.
And similarly, you also have a map put as well as map delete methods
to receive the put and delete requests.
We already know when to use what type of request.
For retrieving the information, such as getting the all products from the database,
we use get method.
I mean, you will make a get request to the server.
For inserting the data, you will make a post request.
To update the data, we make put request.
To delete the data, we make delete request.