Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write custom actionResult in asp.net core

Tags:

In webApi2 i could write a custom ActionResult by inheriting IHttpActionResult.

Sample :

public class MenivaActionResult : IHttpActionResult     {         private readonly HttpRequestMessage _request;         private readonly ServiceResponseBase _response;          public MenivaActionResult(HttpRequestMessage request, ServiceResponseBase response)         {             _request = request;             _response = response;         }           public Task<HttpResponseMessage> ExecuteAsync(CancellationToken token)         {              HttpResponseMessage httpresponseMessage = _response.Exception != null                 ? _request.CreateErrorResponse((HttpStatusCode)_response.HttpCode,                     _response.Exception.Message+":"+_response.Exception.HelpCode)                 : _request.CreateResponse((HttpStatusCode)_response.HttpCode, _response.Data);             return Task.FromResult(httpresponseMessage);         }     } 

The ServiceResponseBase class object holds all the exception and data from service layer..

How i can port this code to asp.net core. I tried but i dont know how create a response message from HttpRequestMessage object in .net core. The IActionResult only have Task ExecuteResultAsync(ActionContext context) method. how i can modify the response from this method

like image 262
Binson Eldhose Avatar asked Mar 04 '17 09:03

Binson Eldhose


People also ask

What is ActionResult ASP NET core?

ASP.NET Core includes the ActionResult<T> return type for web API controller actions. It enables you to return a type deriving from ActionResult or return a specific type. ActionResult<T> offers the following benefits over the IActionResult type: The [ProducesResponseType] attribute's Type property can be excluded.

What is difference between IActionResult and ActionResult?

IActionResult is an interface and ActionResult is an implementation of that interface. ActionResults is an abstract class and action results like ViewResult, PartialViewResult, JsonResult, etc., derive from ActionResult.

Can we use ActionResult in Web API?

Leverage action results to return data as an HttpResponseMessage object from your Web API controller method. ASP.Net Web API is a lightweight framework used for building stateless and RESTful HTTP services. You can take advantage of Action Results in Web API to return data from the Web API controller methods.


1 Answers

Here is an example:

public class TestResult {     public Exception Exception { get; set; }     public object Data { get; set; } }  public class TestActionResult : IActionResult {     private readonly TestResult _result;      public TestActionResult(TestResult result)     {         _result = result;     }      public async Task ExecuteResultAsync(ActionContext context)     {         var objectResult = new ObjectResult(_result.Exception ?? _result.Data)         {             StatusCode = _result.Exception != null                 ? StatusCodes.Status500InternalServerError                 : StatusCodes.Status200OK         };          await objectResult.ExecuteResultAsync(context);     } } 

ObjectResult is the type your results are converted if you return a non-IActionResult from an action. It will do content negotiation for you.

You could also inherit from ObjectResult and setup the status code and data to be written in the constructor.

More on content negotiation in ASP.NET Core: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/formatting#content-negotiation

like image 119
juunas Avatar answered Sep 21 '22 08:09

juunas