This series of articles is to discuss the Action Results for ASP.NET (Core) MVC and Web API.
- Controller Action Result (1), in ASP.NET MVC
- Controller Action Result (2), in ASP.NET (Core) MVC --- this article
- Controller Action Result (3), in ASP.NET Web API
- Controller Action Result (4), in ASP.NET (Core) Web API
This article discusses Controller Action Results for ASP.NET MVC.
A - Introduction
This article is a following up for the previous article, Controller Action Result (1), in ASP.NET MVC to cover the situation of ASP.NET Core MVC. This article actually has the same conclusion as previous one, with emphasis to Core situation. This article will include the following content:
- A - Controller
- B - Controller Action Workflow
- C - Action Results (Core)
- ActionResult Classes (Core)
- ActionResult Categories (Core)
- All Types of Action Methods in ASP.NET Core MVC
B - Controller Action Workflow
Action Results
An action result is what a controller action returns in response to a browser request.
MVC framework includes various Result classes, which can be returned from an action method. The result classes represent different types of responses, such as HTML, file, string, JSON, javascript, etc. The following list all the result classes available in ASP.NET MVC, ASP.NET Core MVC, ASP.NET Web API, and ASP.NET Core Web API.
- Action Results in Web API 2 --- MS, ASP.NET Web API

- Controller action return types in ASP.NET Core web API --- MS, ASP.NETCore 5.0 Web API

- Controller action return types in ASP.NET Core web API --- MS, ASP.NET Core 7.0 Web API

- Action Results in Web API 2 --- MS

Consider using ASP.NET Core web API. It has the following advantages over ASP.NET 4.x Web API:
- ASP.NET Core is an open-source, cross-platform framework for building modern, cloud-based web apps on Windows, macOS, and Linux.
- The ASP.NET Core MVC controllers and web API controllers are unified.
- Architected for testability.
- Ability to develop and run on Windows, macOS, and Linux.
- Open-source and community-focused.
- Integration of modern, client-side frameworks and development workflows.
- A cloud-ready, environment-based configuration system.
- Built-in dependency injection.
- A lightweight, high-performance, and modular HTTP request pipeline.
- Ability to host on Kestrel, IIS, HTTP.sys, Nginx, Apache, and Docker.
- Side-by-side versioning.
- Tooling that simplifies modern web development.
- Types Of Web API Action Results --- c-sharpcorner



- All about types of Action Results in the Web API --- Dhananjay
- void
- HttpResponseMessage
- IHttpActionResult
- Other types, such as IEnumerable, object, collections, etc.

Samples
- IHttpActionResult vs HttpResponseMessage - Geeksblood
- In web API 1, We have a type called HttpResponseMessage for returning Http response message from action method. It is easy to use but it implicates a lot of smaller details to create a response and some extra code on the controller.
- In Web API 2, IHttpActionResult comes as a new feature which is basically the replacement of HttpResponseMessage. It creates a clean code and allows extensibility of the type of response that we create.
Advantages of using the IHttpActionResult:
- It simplifies unit testing of controllers.
- It moves the common logic for creating HTTP responses into separate classes.
- It makes the controller action method clearer, by hiding the low-level details of constructing the response.
- IHttpActionResult contains a single method, ExecuteAsync, which asynchronously creates an HttpResponseMessage instance.
- The advantages of IHttpActionResult in Web API 2
Web API 2 introduced the new interface IHttpActionResult to return back REST responses, where Web API 1 was using the class HttpResponseMessage to represent HTTP response, and HTTPResponseException to represent HTTP response error.
IHttpActionResult allows developers to enhance their Web API 1 code to be :
- More testable.
- More reusable.
- Cleaner, and more elegant.
We are going to unleash the potential of this interface and see some creative ideas on how to use it.
- Kill Your Tech Interview (Web API) --- fullstack.cafe


References:

Join the conversation! Your thoughts help the community grow.