There are 3 ways to return the data from action method in Asp.Net Core Web API.
- Specific type
- IActionResult
- ActionResult<T>
Let's learn all of them one by one.
Return the Specific Type from Asp.Net Core Web API action method
Specifc type is used to return the
Primitive (string, boolean, int, etc) or Complex data (Custom object type) from an action method.
Return the string type from action method in asp.net core web API,
- [Route("website/name")]
- public string GetWebsiteName()
- {
- return "Website ABC";
- }
Return a complex data from action method in asp.net core web API,
- [Route("emplpyees/{id}")]
- public Employee GetEmployeeById(int id)
- {
- var employee = new Employee() { Id = 1, Name = "Nitish" };
- return employee;
- }
Specific type allows us to return,
- Any primitive data types from action method
- Any complex data object
- Collection of objects (like List<T> etc)
- IEnumerable<T>
- IAsyncEnumerable<T>
- etc.
Benifits of using Specific type
While using the swagger or similar application then there is no need to define
ProducesResponseType because we have defined the return type explictly.
Drawback of using Specific type
You cannot return multiple types of data, let's say NotFound, OK, Redirect etc.
Return IActionResult type from Asp.Net Core Web API action method
This IActionResult is an Interface and it is very powerful because it allows us to return multiple types. We can return the data using some buit-in methods.
like,
- OK()
- NotFound()
- Content()
- File()
- Etc.
Let's return the employee data using IActionResult type
- [Route("emplpyees/{id}")]
- public IActionResult GetEmployeeById(int id)
- {
- if (id == 0)
- {
- return NotFound();
- }
- var employee = new Employee() { Id = 1, Name = "Nitish" };
- return Ok(employee);
- }
In the above code, you can notice that we are returning two different types.
Benifits of using IActionResult type
It allows us to return multiple types of data along with the status code, this is very important for RESTful APIs
Drawback of using IActionResult type
Because of its ability to return multiple types of data the swagger would not be able to identify the output so we need to use the ProducesResponseType explictly.
- [Route("emplpyees/{id}")]
- [ProducesResponseType(StatusCodes.Status201Created, Type = typeof(Employee))]
- [ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(Employee))]
- public IActionResult GetEmployeeById(int id)
- {
- if (id == 0)
- {
- return NotFound();
- }
- var employee = new Employee() { Id = 1, Name = "Nitish" };
- return Ok(employee);
- }
Return ActionResult<T> type from Asp.Net Core Web API action method
ActionResult<T> was introduced in Asp.Net Core 2.1 and it can resturn both specific type as well as the built-in (Asp.Net Core) methods.
Let's return the primitive data type and not found data using ActionResult<T> type,
- [Route("website/{websiteId}/name")]
- public ActionResult<string> GetWebsiteName(int websiteId)
- {
- if (websiteId == 0)
- {
- return NotFound();
- }
- return "Website ABC";
- }
We can also return any complex data object using ActionResult<T> type
- [Route("emplpyees/{id}")]
- public ActionResult<Employee> GetEmployeeById(int id)
- {
- if (id == 0)
- {
- return NotFound();
- }
- var employee = new Employee() { Id = 1, Name = "Nitish" };
- return employee;
- }
Benifits of using ActionResult<T> type
- It allows to return multiple types of data along with the status code
- Since we are definng the return type explictly, there is no need to use Type in ProducesResponseType attribute.
- [Route("emplpyees/{id}")]
- [ProducesResponseType(StatusCodes.Status201Created)]
- [ProducesResponseType(StatusCodes.Status400BadRequest)]
- public ActionResult<Employee> GetEmployeeById(int id)
- {
- if (id == 0)
- {
- return NotFound();
- }
- var employee = new Employee() { Id = 1, Name = "Nitish" };
- return employee;
- }
Drawback of using ActionResult<T> type
Still the use of ProducesResponseType is required for status codes.