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.

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.

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

action result type

Samples

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: