Introduction
This article of the series “Web API with ASP.NET Core” will focus on topics like returning HTTP Status Codes from API, their importance, and returning sub resources. We learned how to create an API in ASP.NET Core and how to return resources, in last article, and paused at Status Codes. We’ll continue to explore the importance of status codes and practical examples as well. We’ll also explore resource creation and returning the child resources as well in this article. We can use the same source code as we got at the completion of last article of the series.
HTTP Status Codes

Image credit: https://pixabay.com/en/error-not-found-404-lego-mistake-2129569/
While consuming an API, an HTTP Request is sent and in return, the response is sent along with the return data and a HTTP code. The HTTP Status Codes are important because they tell consumers about what exactly happened to their request; A wrong HTTP code can confuse the consumer. A consumer should know via the response if his/her request has been well taken care of or not, and if the response is not as expected, the Status Code should tell the consumer where the problem is - if it is at consumer level or at API level?
Suppose there is a situation where consumer gets response as status code 200 but at the service level, there is some problem or issue. In that case, consumer will get a false assumption of everything being fine, whereas that won’t be a case. So if there is something wrong at service or there occurs some error on the server, the status code 500 should be sent to consumer, so that the consumer knows that there actaully is something wrong with the request that it sent. In general, there are a lot of access codes. One can find the complete list here, but not all are so important except the few. Few status codes are very frequently used with the normal CRUD operations that a service performs, so service does not necessarily have to support all of them. Let’s have a glance over a few of the important status codes.

When we talk about the levels of status codes, there are 5 levels. Level 100 status codes are more of informal nature. Level 200 status codes are specifically for request being sent well. We get 200 codes for success of a GET request, 201 if a new resource has been successfully created. 204 status codes is also for success but in return it does not returns anything, just like if consumer has performed delete operation and in return doesn’t really expect something back. Level 300 http status codes are basically used for redirection, for example to tell a consumer that the requested resource like page, image has been moved to another location. Level 400 status codes are meant to state errors or client error for e.g. status code 400 means Bad Request, 401 is Unauthorized that is invalid authentication credentials or details have been provided by the consumer, 403 means that authentication is a success, but the user is not authorized. 404 are very common and we often encounter which mean that the requested resource is not available. Level 500 are for server errors. Internal Server Error exception is very common, that contains the code 500. This error means that there is some unexpected error on the server and client cannot do anything about it. We’ll cover how we can use these HTTP status codes in our application.
RoadMap
We’ll follow a roadmap to learn and cover all the aspects of Asp.net core in detail. Following is the roadmap or list of articles that will cover the entire series.
- Create API with ASP.Net Core (Day 1): Getting Started and ASP.NET Core Request Pipeline
- Create API with ASP.Net Core (Day 2): Create an API and return resources in ASP.NET Core
- Create API with ASP.Net Core (Day 3): Working with HTTP Status Codes and returning sub resources in ASP.NET Core API
- Create API with ASP.Net Core (Day 4): Working with Serializer Settings and Content Negotiation in ASP.NET Core API
- Create API with ASP.Net Core (Day 5): Understanding Resources in ASP.NET CORE API
- Create API with ASP.Net Core (Day 6): Inversion of Control and Dependency Injection in ASP.NET CORE API
- Create API with ASP.Net Core (Day 7): Getting Started with Entity Framework Core
- Create API with ASP.Net Core (Day 8): Entity Framework Core in ASP.NET CORE API
HTTP Status Codes Implementation
Let’s try to tweak our implementation, and try to return HTTP Status Codes from API. Now in the EmployeesController, we need to return the JSON result along with the status code in response. Right now we return only the json result as shown below.

If we look closely at JsonResult class and press F12 to see its definition, we find that it is derived from ActionResult class which purposely formats the object in the form of a JSON object. ActionResult class implements IActionResult interface.

Ideally, API should not only return JSON all the time, they should return what consumer is expecting i.e. via inspecting the request headers, and ideally we should be able to return the Status Code also along with the result.
We’ll see that it could also be possible with the JSonResult that we get in return. So, if you assign the JSON result that we create to any variable, say “employees”, you can find the StatusCode property associated with that variable,and we can set that StatusCode property.

We can set the status code to 200 and return this variable, as shown below.
- [HttpGet()]
- public JsonResult GetEmployees()
- {
- var employees= new JsonResult(EmployeesDataStore.Current.Employees);
- employees.StatusCode = 200;
- return employees;
- }
But that would be a tedious job to do everytime. There are predefined methods in ASP.NET core that create IActionResult, and all those are mapped to correct status codes i.e. NotFound if the resource doesn’t exis or BadRequest for erroneous requests, etc.
So, replace the return type of methods with IActionResult instead of JsonResult, and in the GetEmployee method make the implementation as if there is a request of Employee with the id that does not exist, and we can return NotFound , otherwise Employee data with the status code OK. In a similar way for the first method where we return the list of employees, we can directly return Ok with the data. There is no scope of NotFound in this method, because even if no records exist an empty list could be returned with HTTP Status Code OK. So, our code becomes something like shown below,
- using Microsoft.AspNetCore.Mvc;
- using System.Linq;
- namespace EmployeeInfo.API.Controllers
- {
- [Route("api/employees")]
- public class EmployeesInfoController : Controller
- {
- [HttpGet()]
- public IActionResult GetEmployees()
- {
- return Ok(EmployeesDataStore.Current.Employees);
- }
- [HttpGet("{id}")]
- public IActionResult GetEmployee(int id)
- {
- var employee = EmployeesDataStore.Current.Employees.FirstOrDefault(emp => emp.Id == id);
- if (employee == null)
- return NotFound();
- return Ok(employee);
- }
- }
- }
Compile the application and run it. Now, go to postman and try to make requests. In the last article, we made a request of an employee with the Id 8, that does not exist and we got the following result.

The result said null, that is not ideally a correct response. So try to make the same request now with this new implementation that we did. In this case, we get a proper status 404 with Not Found message as shown below,

Since we know that we can also send the request from web browsers as browsers support HTTP Request. If we try to access our API via browser, we get an empty page with the error or response in the developer’s tool as shown below.

We can make our responses more meaningful by displaying the same on the page itself as ASP.NET Core contains a middle ware for Status Codes as well. Just open the configure method of the startup class and add following line to the method.
- app.UseStatusCodePages();
That is adding it to our request pipe line. So the method will look like,
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
- {
- loggerFactory.AddConsole();
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- else
- {
- app.UseExceptionHandler();
- }
- app.UseStatusCodePages();
- app.UseMvc();
- //app.Run(async (context) =>
- //{
- // throw new Exception("Test Dev Exception Page");
- //});
- //app.Run(async (context) =>
- //{
- // await context.Response.WriteAsync("Hello World!");
- //});
- }
Now again make the same API request from browser and we get the following message on the browser page itself.

Returning Sub Resources
In the EmployeeDto, we have a property named NumberOfCompaniesWorkedWith, considering this as a sub resource or child resource, we might want to return this as a result as well. Let’s see how we can achieve that. Add a DTO class named NumberOfCompaniesWorkedDto to the Model folder, and add Id, name and Description to that newly added class.

NumberOfCompaniesWorkedDto.cs
- namespace EmployeeInfo.API.Models
- {
- public class NumberOfCompaniesWorkedDto
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Description { get; set; }
- }
- }
Now in the EmployeeDto class add a property w.r.t. this NumberOfCompaniesWorkedDto that returns the collection of all companies an employee has worked with.
EmployeeDto.cs
- using System.Collections.Generic;
- namespace EmployeeInfo.API.Models
- {
- public class EmployeeDto
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Designation { get; set; }
- public string Salary { get; set; }
- public int NumberOfCompaniesWorkedWith
- {
- get
- {
- return CompaniesWorkedWith.Count;
- }
- }
- public ICollection<NumberOfCompaniesWorkedDto> CompaniesWorkedWith { get; set; } = new List<NumberOfCompaniesWorkedDto>();
- }
- }








biswa nayakPosted Jul 19, 2017, 12:13 PM
Sir can u share some post methode example code in .net core
Mukesh ShahPosted Jun 7, 2017, 11:53 PM
Very Nice Description....
Thiruppathi RPosted Jun 7, 2017, 12:26 AM
Thanks for sharing,...