Return Custom HTTP Status Code from WebAPI 2 endpoint

C#asp.net Web-ApiHttp Status-Code-404

C# Problem Overview


I'm working on a service in WebAPI 2, and the endpoint currently returns an IHttpActionResult. I'd like to return a status code 422, but since it's not in the HttpStatusCode enumeration, I'm at a loss as to how it would be sent, since all of the constructors require a parameter of HttpStatusCode

As it stands now, I'm returning BadResult(message), but returning a 422 + message would be more descriptive and useful for my clients. Any ideas?

C# Solutions


Solution 1 - C#

According to C# specification:

> The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type and is a distinct valid value of that enum type

Therefore you can cast status code 422 to HttpStatusCode.

Example controller:

using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CompanyName.Controllers.Api
{
    [RoutePrefix("services/noop")]
    [AllowAnonymous]
    public class NoOpController : ApiController
    {
        [Route]
        [HttpGet]
        public IHttpActionResult GetNoop()
        {
            return new System.Web.Http.Results.ResponseMessageResult(
                Request.CreateErrorResponse(
                    (HttpStatusCode)422,
                    new HttpError("Something goes wrong")
                )
            );
        }
    }
}

Solution 2 - C#

 return Content((HttpStatusCode) 422, whatEver);

credit is for: https://stackoverflow.com/questions/28588652/return-content-with-ihttpactionresult-for-non-ok-response

and your code must be <= 999

and please ignore codes between 100 to 200.

Solution 3 - C#

I use this way simple and elegant.

public ActionResult Validate(User user)
{
     return new HttpStatusCodeResult((HttpStatusCode)500, 
               "My custom internal server error.");
}

Then angular controller.

function errorCallBack(response) {            
$scope.response = {
   code: response.status,
   text: response.statusText
}});    

Hope it helps you.

Solution 4 - C#

Another simplified example:

public class MyController : ApiController
{
    public IHttpActionResult Get()
    {
        HttpStatusCode codeNotDefined = (HttpStatusCode)422;
        return Content(codeNotDefined, "message to be sent in response body");
    }
}

Content is a virtual method defined in abstract class ApiController, the base of the controller. See the declaration as below:

protected internal virtual NegotiatedContentResult<T> Content<T>(HttpStatusCode statusCode, T value);

Solution 5 - C#

For this you may need to use an Action Filter Attribute. Nothing fancy. Just create a class and inherit it from ActionFilterAttribute class in c#. Then override a method named OnActionExecuting to implement this. Then just use this filter on the head of any controller. Following is a demo.

On the condition when you need to produce custom status code based message in ActionFilterAttribute you can write in following way:

        if (necessity_to_send_custom_code)
        {
            actionContext.Response = actionContext.Request.CreateResponse((HttpStatusCode)855, "This is custom error message for you");
        }

Hope this helps.

Solution 6 - C#

I'm using a custom Filter in my controllers that catches any exceptions and returns custom error results so I needed a way to return a custom message response based on the exception message.

I'm using JsonResult (found in the Microsoft.AspNetCore.Mvc namespace) to return a custom status code and messagesas such:

public class DomainViolationFilter : ExceptionFilterAttribute
    {
        public override void OnException(ExceptionContext context)
        {
            switch (context.Exception)
            {
                case EntityNotFoundException e:
                    JsonResult toReturn = new JsonResult(e);
                    toReturn.StatusCode = 404;
                    context.Result = toReturn;
                    return;
            }
        }
    }

In my case, I'm serializing directly the exception object (ex) for simplicity but you should be returning only the relevant (and safe) data, or whatever you need in your use case.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGarrison NeelyView Question on Stackoverflow
Solution 1 - C#lilo.jacobView Answer on Stackoverflow
Solution 2 - C#peymanView Answer on Stackoverflow
Solution 3 - C#Ricardo G SaraivaView Answer on Stackoverflow
Solution 4 - C#themefieldView Answer on Stackoverflow
Solution 5 - C#Sajeeb Chandan SahaView Answer on Stackoverflow
Solution 6 - C#Filipe MadureiraView Answer on Stackoverflow