Lecture thumbnail 0:00 / 0:00 By default, you have to use the response.writeAsync method to send some content as a response to the
browser. In addition to that, in case if you want to send a particular status code other than 200,
you have to write another statement response.statusCode equal to some particular
statusCode value. In order to minimize this work and simplify the same in shortcut way,
the IResult interface has been introduced. Here IResult is the parent interface which has
implementations, for example Ok, json, text, file etc. as you can see on the screen.
So these are all the different implementations of IResult.
By returning any of these, you can return particular type of status code
with or without the content. Let me show that with an example.
Suppose in this particular map delete example, we would like to provide an error response,
for example bad request with incorrect product id.
See first of all, the user supplies the id in this route parameter and we are receiving the
parameter in this lambda expression and then we are trying to search for the particular product
based on the id. But if it is not found, it comes here and we have to provide 400 with content.
So instead of this, you can write a statement return results.BadRequest.
Here bad request is one of the implementations of IResult.
See the data type of bad request. It returns IResult.
Optionally, you can supply an error message as a string or modal object.
That will be the response body. For example, I would like to send this message as response body.
You can also send a modal object as response. It is up to the scenario.
For example, you can write an object, a new anonymous object
with a property called message equal to something. In this case, this anonymous object will be
automatically converted into JSON format and that JSON object will be sent as response to the browser.
So this single line of code is equivalent to the above two lines.
So we can get rid of that. Similarly, in case of successResponse,
instead of writing response.writeAsync as an alternative, you can write a return a result of
yes, it is Ok result because it is success. This Ok is also another implementation of IResult
and in this case, the status code will be 200 and optionally a string or object as response.
Here, let’s say for example, product deleted. This is the response.
You can also supply a modal object or anonymous object.
In that case, the same will be converted into JSON format first on the server
and that JSON object will be added into the response body that can be sent to the client.
For example, just like above, we are writing an object with message equal to the message value
and above we can say error. Ok, let me check that.
Ok, in this case, we are trying to make a delete request and the URL is products slash one.
Request verb is delete. Send the request.
Now you can see the JSON format response that is message equal to product deleted.
This is successResponse. So status code is 200.
But in case if the product id is incorrect, for example, let’s say four,
which does not exist in the actual collection. Now as you can see, we have got error.
Again, still in JSON format. So in the return statement,
if you gave a C sharp modal object, the same will be converted into JSON format automatically
on the server side itself and that JSON information will be sent as response to the client.
And in this case, the status code is 400 because it is bad request.
So in this example, we have used results.Ok and results.BadRequest.
Similarly, we also have other implementations. For example, results.Ok where the status code
will be 200 and the content type will be by default application by JSON or text by plain,
depending on the object that you gave in the response.
Here the response object can be either string or modal object
or even can be modal array or modal collection.
In case of modal collection object, that object will be converted into JSON format
as particularly JSON array. For example, list of products as array.
But in case if you want to return only JSON format in particular,
then you can use a specialized method called results.json.
Here also the status code will be 200. But in this case, you must give only objects.
That means the strings are not allowed here. That’s the difference between these two.
In case of Ok, string or modal object is allowed,
but strings are not allowed in case of results.json. And in the same way, we have results.text
which is exclusively meant for string type of responses.
Ok, in general, results.Ok is commonly used because it works for strings as well.
In case if you want to return the file stream as a response which can be downloadable in the client,
for example, file attachment such as images or executable files,
in that case, you can send the file stream as a response by using results.file.
It is almost equivalent to file stream result that we have learned in the controllers.
Similarly, we have a bad request where the status code will be 400 automatically
and the response object can be a string or modal object,
means a simple plain error message as a string. Or you can also send a modal object
that contains error details along with any additional details in particular if required.
For example, the error message is invalid product ID.
Optionally, you can also supply the ID value as another property.
And in case if you want to return 404 not found, you will use results.notfound
with or without response value. And for unauthorized, that means the user is not
authenticated or authorized, in that case, you will use 401, unauthorized.
And particularly for validation errors, you can also try using results.validation problem.
In this case, a JSON object will be automatically created
with modal object information along with validation errors. Let me show that practically.
For example, here, instead of you use the bad request, you can also try writing validation
problem. In this case, we have to return a dictionary of string and string array.
Here, the key must be the parameter which is having the validation error.
In this case, it is the parameter called ID. So that is the key here. And the value is,
it must be string array with error message. So in this particular format only, we have to send
the response with error messages. So this is totally one key value pair
where the ID is the name of the parameter that is the key. And the value of the dictionary is
a string array which contains one or more error messages. So keep it in curly brackets.
This is one dictionary element. You can add more than one. So we have just given the name of the
parameter and corresponding one or more error messages as a string array. The validation problem
method accepts only this type of argument, a dictionary of string and string array.
But you need to see the response that is generated by the validation problem.
For example, the URL is products slash for which does not exist in the collection.
So incorrect product ID. It causes bad request. But see, this is the response that you are getting.
This type of JSON format will be automatically generated. It includes with the type and it is
just some URL. Ok, ignore it. But this is the title. This title is automatically generated
and of course customizable too. And the status code is 400, bad request.
And that dictionary object is included as a JSON array here.
ID is the name of the parameter that has validation errors.
For example, you might have more than one parameters, for example, three or four,
out of which the particular parameter has the validation error.
So we are including that parameter name that has validation errors.
And this is the list of validation errors of the particular parameter.
So this type of JSON format will be automatically generated because of the validation problem
result type. If it is suitable for your workflow, you can use it.
Otherwise, the built-in bad request also works fine.
So it is a choice between bad request and validation problem
for serving the validation errors as response.
Ok, we have used result types in the map delete method.
But you have to apply the same to the other endpoints,
such as map get as well as map post and put. So it is an assignment for you.
Try changing the remaining responses also into result types.
For example, here you can write like results.Ok, here results.BadRequest, like that.
And in the GitHub source code of this lecture,
you can find the updated source code after updating all these responses into a result.
So you can use that source code for your reference.