This series of articles is to discuss the Action Results for ASP.NET (Core) MVC and 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:

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.

  1. void
  2. HttpResponseMessage
  3. IHttpActionResult
  4. Other types, such as IEnumerable, object, collections, etc.

Advantages of using the IHttpActionResult:

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 :

  1. More testable.
  2. More reusable.
  3. Cleaner, and more elegant.

We are going to unleash the potential of this interface and see some creative ideas on how to use it.

References: