ASP.NET Core is a cross-platform, open-source, bias, rapid, and modular framework for developing high performance web applications. There are a few different ways in which you can occupy parameters for action methods in ASP .NET Core MVC. You can pass them by URL, query string, request header or body, or even a form.
All public methods of the control class are known as action methods. Because theyre created for a specific action or operation in the application. Therefore, the controller class can have several related action methods. As an example, adding a student is an action. Modifying student data is another action. Deleting a student is another action. So, the point you need to remember is that all related actions should be created inside a particular controller.
The action method can return many types. Lets modify the HomeController as shown below where we have a method that gives all the details to the students. Intentionally we returned the string from this method but as we progress in this course, we will discuss the actual implementation of this method. But for now, we are going to return the string just for learning.
using Microsoft.AspNetCore.Mvc; namespace MVCWebApplication.Controllers { public class StudentController : Controller { public string GetAllStudents() { return "Return All Students"; } } }
Starting from .NET Core version 2.1, there are still 3 types of WebApi responses that controller actions can return. All three types had their own pros and cons but all have lacked options to satisfy both REST and high level of testing.
There are three ways to return the data from action method in Asp.Net Core Web API.
Lets learn it all one by one.
The specific type is used to restore primitive (Boolean, Integer, String etc.) or tangled data from the action method.
Return string type from action method in Asp.net Core Web API,
[Route("website/name")] public string GetWebsiteName() { return "Website XYZ"; }
Return a composite data from the action method in Asp.net Core Web API,
[Route("emplpyees/{id}")] public Employee GetEmployeeById(int id) { var getEmployee = new Employee() { Id = 1, Name = "ABC" }; return employee; }
The special type allows us to return,
There is no need to define the ProducesResponseType when using Swagger or a similar application as we have defined the return type.
You cant return multiple types of data, lets say NotFound, ok, redirect, etc.
ActionResult types represent different HTTP status codes and the IActionResult return type is suitable when numerous ActionResult return types are feasible in action. Any non-abstract class deriving from ActionResult is appropriate as a valid return type. A few common return types in this classification are BedRequestResult (400), OkObjectResult (200), and NotFoundResult (404). Alternatively, feature methods in the ControllerBase class can be used to return ActionResult types from the action.
The IActionResult is an interface and it is very powerful because it allows us to return multiple types. We can return data using some built-in methods like,
Lets return the employee data using the IActionResult type
[Route("emplpyees/{id}")] public IActionResult GetEmployeeById(int id) { if (id == 0) { return NotFound(); } var getEmployee = new Employee() { Id = 1, Name = "ABC" }; return Ok(getEmployee ); }
In the code above, you can notice that we are returning two different types.
It allows us to return multiple types of data with status code and it is very important for RESTful API.
We need to use the ProducesResponseType explicitly because Swagger is not able to identify the output due to its ability to return multiple types of data.
[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 getEmployee = new Employee() { Id = 1, Name = "ABC" }; return Ok(getEmployee ); }
Return the ActionResult type from Asp.Net Core web API action method
ActionResult was introduced in Asp.net Core 2.1 and it can resume both specific types as well as the built-in (Asp.net Core) methods.
Lets go back to the primitive data type and not found data using the ActionResult type,
[Route("website/{websiteId}/name")] public ActionResult GetWebsiteName(int websiteId) { if (websiteId == 0) { return NotFound(); } return "Website XYZ"; }
We can also return any complex data return using the ActionResult type
[Route("emplpyees/{id}")] public ActionResult GetEmployeeById(int id) { if (id == 0) { return NotFound(); } var getEmployee = new Employee() { Id = 1, Name = "ABC" }; return getEmployee ; }
It allows to return multiple types of data with the status codes.
We are explicitly specifying the return type, so there is no need to use the Type in ProducesResponseType attribute.
[Route("emplpyees/{id}")] [ProducesResponseType(StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status400BadRequest)] public ActionResult GetEmployeeById(int id) { if (id == 0) { return NotFound(); } var getEmployee = new Employee() { Id = 1, Name = "ABC" }; return getEmployee ; }
Still the ProducesResponseType must be used for the status code.
In this article, we have learned the different ways to return the data from controller action method. Action Result classes in ASP.NET Core MVC provides a significant portion of the functionality that you will be able to use in your controllers. They return status codes, objects, files, other content and even redirect clients. As you become more familiar with ASP.NET Core MVC and its functionality. Although you might find it more comfortable to return specific type in the controller action, you may have your unit tests which do not cover your code properly and so can take the opening potential bus in the future.
July 29, 2021
July 22, 2021
July 20, 2021
July 16, 2021
Well do everything we can to make our next best project!
Check out our most recent blogs
July 29, 2021
What is Angular? Angular is a frontend development framework used for building single-page client applications using HTML and Typescript. It is written in Typescript. What...
July 22, 2021
What is a Compiler? A compiler is nothing but a part of code that converts one programming language to another. If we talk about some simple programming languages like C, C++,...
July 20, 2021
Introduction Angular is a remarkable framework that may be used to create mobile and desktop web apps with spectacular UIs. It's made with JavaScript. we can utilize HTML, CSS,...
Well do everything we can to make our next best project!