Lecture thumbnail 0:00 / 0:00 The appropriate application for this is, you can perform the CRUD operations with this code.
For example, let’s say, there is a requirement of creating CRUD operations for the products.
So, if you make a GET request for this URL called products,
it has to provide the list of existing products.
Let’s try the same.
First, let us try to create a model class for the same.
Right-click on this project.
Add new folder.
Models.
So, we are going to create a model class for the same.
Select class.
Class name is Product.
Add button.
So, in this particular class, add essential properties
such as product id and product name.
OK, remember these property names, id and product name.
OK.
Now, in the program.cs file, let’s try to create a list of products.
Actually, for better architecture, we have to create this kind of data in the repository,
and we have to call that particular repository in the service,
and call that service within this minimal API.
But we are not going to do that.
To keep the example as simple as possible.
And import the namespace.
MinimalAPI.Models.
OK, in the program.cs file, we are creating a dummy set of products.
For example, two products.
OK, these two are just examples.
And we have to provide this data as JSON format for this URL.
So, when we make a request to products.
This is the route.
And we are receiving GET request in this case.
Now, instead of hello world, we have to provide the list of products as response.
But by default, it is the list of objects.
We have to read each of them.
And call the ToString method of the same for every product.
And finally, we have to convert the same into a plain string.
Separated with a backslash n, a newline character.
So, we are reading each product from this collection, calling ToString method of the same.
So, this will return a list of strings.
And for that list of strings, we are using join.
I mean, it works like, for example, this is the first string.
Then product name.
Then on the next line, second product and another product name and so on.
So, this is the expected functionality.
And we have to store this content in a variable.
And send the same as response.
OK.
So, we have generated each product into product id as well as name.
And sent the same as response to the browser by using this response.writeAsync.
By the way, we have to override this ToString method in the product class.
So, in the product class, override which method?
ToString.
Now here, we have to return a string value that contains both product id and name.
So, we are invoking this ToString method over here.
That is in this select method.
So, this select method calls the ToString method of each product.
And adds them as line by line with separator character called backslash n.
And if we talk about post method,
we are going to add the product into the existing products collection.
So, we will receive a product object as parameter.
Yes, it is possible to use automatic model-binded object in this endpoint as well.
The minimal API supports it.
So, we are receiving the product object.
Hopefully, the post request contains the id and product name.
So, those values are received automatically as a model object.
And we are trying to insert the same into the models collection.
Actually, we have to create a repository method for the same.
And we have to invoke that repository method in the service.
And call that service in this endpoint.
That is the formal way to create the application.
Anyways, for this simple example, let’s keep it simple.
And then the response is product added.
OK, this is our code.
Let’s add the breakpoints.
And run.
Now, let’s try to make a GET request for the URL.
Port number is 5075.
You can see the same in the browser.
And the route is products.
If you make a GET request for the same.
Yes, the request has been received here.
And we are going to generate the content.
Which includes with all the products information.
That is product id as well as product name.
This is the result of the ToString method.
And we are calling the ToString method for each product.
So, in this way, this is the output.
So, this will be sent as response to the browser.
And you can see the same response over here.
That’s fine.
Now, let’s try to make a POST request.
Selecting POST.
And the URL is same.
But as you can guess, we have to supply the product id and name as a part of the body.
So, I am using the encoding of x-www-form-urlencoded.
We have already learned different types of model binding in the model binding section itself.
So, selecting this form URL encoded.
The key, for example, id.
So, the product id is, for example, 3.
And the product name.
Make sure these keys match with the property names.
Otherwise, model binding cannot read these values.
For example, product name is iPad.
Now, make a POST request over here.
Now, it is saying 405 method not allowed.
But why?
Oops, the route is products, isn’t it?
So, let’s stop the application.
Change the route to products.
Run again.
In the postman, we are making a POST request.
Route is products.
Parameters or id and product name.
Send.
Yes, this is the point.
It doesn’t support the encoding types of form data or www form URL encoded.
That is the current feature and limitation of minimal API.
See, if you create the same type of code with controllers,
it might have supported any of these media types.
Say it is form data or www form URL encoded,
as well as JSON or XML binding.
Everything is by default supported out of the box.
But minimal API only supports either QueryString or JSON format or XML format.
That’s it.
So, you have to switch to raw.
And then within here, you try to make a request to JSON.
So, submit your data as a JSON format.
Alternatively, you can send the data as a part of the QueryString parameters as well.
Okay, in case of POST request, the QueryString parameters is not appropriate.
So, we are selecting JSON from this drop-down menu.
And add your data as a JSON object.
That is, within double quotes id and then the value.
Product name, then the value.
Numbers need not have double quotes in JSON.
Okay, the id, for example, 3.
So, this is your payload in the request, which gets added to the request body.
Now make a request.
So, that request is received here.
You can see the breakpoint hits, because we made a POST request with this route.
And as you can see, we have received the model object.
That is id and name.
So, the question yourself.
Does minimal API has support for model binding?
Yes, it has minimal support or partial support.
It supports the model binding from the QueryString.
But if you talk about model binding from the request body,
it supports only the input sources of JSON format or XML data.
It doesn’t support www form URL encoded or the multi-product form data itself.
Okay, so we have received the product object and sent the same as response.
I mean the message product added.
Now, alternatively, if you make a GET request instead.
Suppose we make a GET request in this case.
No parameters.
So, again, we have received that request over here.
I mean the first endpoint and sending the list of products, including the newest one.
So, we have implemented retrieving and inserting operations.
In the same way, we can also extend this application with updation as well as deletion code.
So, this is what exactly the type of applications that you can create with minimal API.
An application that can only receive the request and can provide only data but not views.
And that is what exactly required for single-page applications,
such as Angular apps or React apps or mobile apps.
But if you can ask me what are the advantages of minimal API over the Web API controllers,
the minimal API has minimal dependency on the ASP.NET Core packages.
So, it is more of lightweight in terms of the packages required from the ASP.NET Core.
It is well suited for small-scale applications or for large-scale applications with microservices.
In general, it is a great option to create microservices.
Microsoft really wants to enhance the microservices experience with minimal API.
That’s the reason from ASP.NET Core version 6 or 7 onwards,
they keep on actively working on improvements in minimal API.
So, all the concepts that we have learned so far up to the 25 sections
are not going to change in the future versions of ASP.NET Core.
You can see certain improvements or changes in minimal API in particular.
OK, this is what the context you should understand.