What are the various return types of a controller action in C# ASP.NET WebAPI?

CsharpServer Side ProgrammingProgramming

The Web API action method can have following return types.

  • Void

  • Primitive Type/Complex Type

  • HttpResponseMessage

  • IHttpActionResult

Void

It's not necessary that all action methods must return something. It can have void return type.

Example

using DemoWebApplication.Models
using System.Web.Http;
namespace DemoWebApplication.Controllers{
   public class DemoController : ApiController{
      public void Get([FromBody] Student student){
         //Some Operation
      }
   }
}

The action method with void return type will return 204 No Content response.

Primitive Type/Complex Type

The action method can return primitive type like int, string or complex type like List etc.

Example

using DemoWebApplication.Models;
using System.Collections.Generic;
using System.Web.Http;
namespace DemoWebApplication.Controllers{
   public class DemoController : ApiController{
      public List<string> Get([FromBody] Student student){
         return new List<string>{
            $"The Id of the Student is {student.Id}",
            $"The Name of the Student is {student.Name}"
         };
      }
   }
}

HttpResponseMessage

Example

HttpResponseMessage is used when we want to customize the return type (action result) of an action method. Responses are customized by providing status code, content type, and data to be returned to HttpResponseMessage.

using DemoWebApplication.Models;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace DemoWebApplication.Controllers{
   public class DemoController : ApiController{
      public HttpResponseMessage Get([FromBody] Student student){
         if(student.Id > 0){
            return Request.CreateResponse(HttpStatusCode.OK, $"The Sudent Id is
            {student.Id} and Name is {student.Name}");
         } else {
            return Request.CreateResponse(HttpStatusCode.BadRequest, $"InValid
            Student Id");
         }
      }
   }
}

In the above example we could see that the Response is customized. Since the Id which is sent to the action method is 0, the else is executed and 400 Bad request is returned with the error message provided.

IHttpActionResult

Example

The IHttpActionResult interface was introduced in Web API 2. Essentially, it defines an HttpResponseMessage factory. IHttpActionResult is present in System.Web.Http namespace. The advantages of using IHttpActionResult over HttpResponseMessage are as follows.

  • Simplifies unit testing your controllers.

  • Moves common logic for creating HTTP responses into separate classes.

  • Makes the intent of the controller action clearer, by hiding the low-level details of constructing the response.

Example

using DemoWebApplication.Models;
using System.Collections.Generic;
using System.Web.Http;
namespace DemoWebApplication.Controllers{
   public class DemoController : ApiController{
      public IHttpActionResult Get([FromBody] Student student){
         var result = new List<string>{
            $"The Id of the Student is {student.Id}",
            $"The Name of the Student is {student.Name}"
         };
         return Ok(result);
      }
   }
}

raja
Published on 19-Aug-2020 15:21:16
Advertisements