HI WELCOME TO SIRIS

200 HTTP Status Code in ASP.NET Core Web API

Leave a Comment

In this article, I am going to discuss how to return 200 HTTP Status Code from the ASP.NET Core Web API Controller Action method with Examples. Please read our previous article where we give an overview of the HTTP status code. We are going to work with the same example, that we created in our Controller Action Return Types in the ASP.NET Core Web API article.

How to return Proper HTTP Status code?

In order to return the proper status code, the ASP.NET Core Framework provided some built-in methods, and using those methods you can format your response data. And the best thing is that you don’t need to remember all these methods. What you need to do is, simply right-click on the ControllerBase class and choose to go to definition. And here, you can see this is an abstract class with lots of properties and methods as shown in the below image.

How to return Proper HTTP Status code?

How to return 200 HTTP Status Code?

This is the most common HTTP Status Code that is going to be returned from the ASP.NET Core Web API controller method. As we already discussed in our previous article, the 200 HTTP Status Code belongs to the Successful category. That means the HTTP 200 Status code indicates that the request is successful.

ASP.NET Core provided the OK method to return HTTP 200 Status Code. If you check the ControllerBase class then you will find two overloaded versions of the OK method as shown below.

How to return 200 HTTP Status Code?

The first overloaded version which does not take any input parameter will create an object that produces an empty HTTP 200 OK Status code as a response. On the other hand, the second overloaded version takes an object (any type of value) as input and creates an object that produces HTTP 200 OK Status code as a response. The input data it takes will be formatted in the response body.

Further, if you notice the first overloaded version of the OK method returns OKResult. Now, right-click on the OkResult and choose to go to definition and you will see the following definition.

200 HTTP Status Code in ASP.NET Core Web API

As you can see in the above image, this method is inherited from the StatusCodeResult class, and further if you right-click on the StatusCodeResult class and choose to go to definition, then you will find the following definition.

The StatusCodeResult class is inherited from ActionResult and IActionResult. So, you can use IActionResult as the return type for the OK method. The StatusCodeResult class has the StatusCode property and using this property you can set the proper status code.

Further, if you notice the second overloaded version of the OK method returns OkObjectResult. Now, right-click on the OkObjectResult class and choose to go to definition and you will see the following definition.

how to return 200 HTTP Status Code from the ASP.NET Core Web API

Here, as you can see, the OkObjectResult class is inherited from the ObjectResult class. Now, again, right-click in the ObjectResult class and choose to go to definition and you will find the following definition of ObjectResult class.

how to return 200 HTTP Status Code from the ASP.NET Core Web API

If you notice here, the ObjectResult class is inherited from ActionResult and IActionResult. So, you can use IActionResult as the return type for the second overloaded version of the OK method. Further, if you notice the ObjectResult class has the StatusCode property and using this property you can set the proper status code. Let us use the above two overloaded versions in our example.

Returning 200 HTTP Status code without data:

In this case, we have to use the first overloaded version of the OK method which does not take any parameter. So, please modify the Employee Controller as shown below.

using Microsoft.AspNetCore.Mvc;
namespace ReturnTypeAndStatusCodes.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
[Route("GetEmployees")]
public IActionResult GetEmployees()
{
return Ok();
}
}
}

Now, run the application and issue a GET request to the URL api/employee/getemployees using postman as shown in the below image. As you can see, once you hit the Send button, you will get a 200 OK Status code without any data in the response body.

Returning 200 HTTP Status code without data

Returning 200 HTTP Status code with data:

In this case, we have to use the second overloaded version of the OK method which takes an object as an input parameter. So, please modify the Employee Controller class as shown below. Here, we are returning the list of employees from the action method. As the OK method takes object type, so, we can pass any data. Here, I am passing the list of employees to the OK method.

using System.Collections.Generic;
using ReturnTypeAndStatusCodes.Models;
using Microsoft.AspNetCore.Mvc;
namespace ReturnTypeAndStatusCodes.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
[Route("GetEmployees")]
public IActionResult GetEmployees()
{
var listEmployees = new List<Employee>()
{
new Employee(){ Id = 1001, Name = "Anurag", Age = 28, City = "Mumbai", Gender = "Male", Department = "IT" },
new Employee(){ Id = 1002, Name = "Pranaya", Age = 28, City = "Delhi", Gender = "Male", Department = "IT" },
new Employee(){ Id = 1003, Name = "Priyanka", Age = 27, City = "BBSR", Gender = "Female", Department = "HR"},
};
return Ok(listEmployees);
}
}
}

Now save the changes and run the application and issue a GET request to the same URL api/employee/getemployees using postman as shown in the below image. Now, you can see, once you hit the Send button, you will get 200 OK Status code with the employee data in the response body in JSON format.

Returning 200 HTTP Status code with data

The OK Method is very helpful for GET type requests. For example, Get all employees data, Get single employee data, Get employee data based on some search parameters, etc.

In the next article, I am going to discuss how to return 201 HTTP Status Code in ASP.NET Core Web API with Examples. Here, in this article, I try to explain, how to return 200 OK HTTP Status Code from ASP.NET Core Web API with Examples and I hope you enjoy this 200 HTTP Status Code in the ASP.NET Core Web API article.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.