Lecture thumbnail 0:00 / 0:00 Just like what we have learned in routing,
you can use the routing parameters and routing
constraints as a part of the minimal API.
So while creating routes using minimal API, you
can add the route parameters as well as
route constraints.
You can use all the constraints such as
int, bool, and others as you can see.
In the routing section earlier, we have already
learned all these things in detail.
OK, let’s try to quickly use them.
For example, the requirement is, if the URL
is products slash product ID, it has to
provide the product name as a response.
So let me create another route for the
same in between here.
I am trying to receive the GET request
and the URL is products slash the product
ID.
Here, ID is the parameter.
So we have to keep in curly brackets.
So in case if you receive the request
at this route, of course, we are receiving
the context object as usual.
And at the same time, we are receiving
the route parameter value.
For example, int ID.
So we are receiving the ID parameter value.
And at the same time, we can read
its value and do something based on that.
For example, we are trying to send the
response.
That is, from the products.
We have to get the product where the
product ID is matching with this parameter value.
So use the link query here.
Whenever the ID is matching with this ID
parameter value, which is the route parameter value,
actually, for example, it can be one.
So if that ID matches with this value
called one, for example, then we have to
get the corresponding product object.
So use the first or default.
First, get the same into a product variable
and then check if it is not null.
Now, first, if it is null, we have
to send the error response.
For example, ID is incorrect, because if the
user supplies invalid value for the product ID,
which does not exist in the database, then
only the product object will be null.
So if it is null, this is the
response.
Also, optionally, set the status code.
For example, a bad request.
Response.statusCode is 400.
That is, bad request.
And this variable is nullable.
So question mark.
So first, we are trying to fetch the
corresponding product object based on the parameter value.
In case if it is not found, we
are trying to send a bad request with
this message.
But in case if the product ID is
valid and we have got a valid product
object into this variable, then we will send
the product object as argument.
Convert the product object into string by using
the toString method and send the same as
response.
OK, this is the code and let’s run.
In this postman, let us try to make
a get request.
URL is products slash some invalid product ID,
for example 3, which does not exist in
the products collection.
Oops, we are getting null reference exception.
Because the product is null.
But if it is null, we are sending
a bad request, isn’t it?
Oops, we have not exit from this lambda
expression.
In order to exit from this lambda expression,
as soon as the product is null, we
have to write return.
So in case if product is null, after
execution of this code, it will not reach
to the subsequent code.
Let’s try again.
Again here, the URL is products slash invalid
product ID, for example 3, and we are
getting incorrect product ID as response, that is
400, bad request, that is as expected.
But if you enter a valid value, for
example 1, and as you can see, we
have received the ID value, that is 1,
and searching in the products collection for the
particular product.
And in this case, the product is not
null.
We have the corresponding product object where ID
equal to 1.
So the first or default method returns the
corresponding product object.
Since the product is not null, this piece
of the code will be skipped, and we
will return the product information as part of
the response.
That’s fine, we have got 1.
But instead of returning the product details as
plain text, we can also return the same
as a JSON format.
Let’s try.
So when you want to convert your object
into JSON format and send the same as
response, we can use the predefined class called
JSONSerializer, that is from system.text.json namespace.
So we have to import that namespace at
the top, system.text.json. And in this
particular class, we have the method called serialize,
and we have to pass the product object
as argument.
So it reads all the properties from that
product object and converts the same into JSON
format.
And we can use the same technique for
remaining methods.
For example, for the above map get.
Instead of using string.join to convert all
products into plain string, instead of this, we
can use the same thing, JSONSerializer.serializeOf the
complete product list.
OK.
Now test it again.
So if you make a request to products,
now you can see all the products information
as a plain JSON format.
This is actually the Angular or React or
any other single-page application libraries want, because
they consume the data in JSON format only.
OK.
If the URL is products slash product ID,
for example, one, it returns only the matching
product object.
So as you can see there, we have
received only the matching product object, a single
object in this case.
In the previous case, it was JSONArray.
OK, that’s fine.
But you wanted to receive only the integer
value in this ID parameter, right?
So we can use the constraint, that is,
int.
So we can use all the constraints, for
example, int, bool, datetime, and as listed here.
OK, it works in the same way.
That’s fine.
It is working.
So overall, the conclusion for this lecture is
the minimal API can receive the model objects.
That means it can receive the input data
in the form of JSON or XML or
as query string, but not as multiproduct form
data or www form URL encoded.
So that model objects can be received as
arguments in this lambda expression, for example, as
product object here.
And also, it can have the route parameters
mentioned as a part of the route.
Optionally, the constraints.
And overall, if we check our progress as
well, in this API application, we have implemented
the scenario of getting all the products, getting
product by product ID, and also inserting the
new product.
We will also implement updation as well as
deletion applications in further lectures in the same
section.