ASP.NET MVC 5 - JsonResult with Http Status Code 400

ASP.NET Web API 2 introduced several helper methods like Ok, NotFound, BadRequest, etc… . This allows us to write code which makes its intentions clear.

In recent times, I have been working in semi-single page application which uses ASP.NET MVC 5 and AngularJS. The application is designed in a way that, there is only one view per feature and each view is SPA. So, each ASP.NET MVC controller have only one GET action that returns the view and rest of the communication is using actions that returns JsonResult that can be used by client in AJAX style just like consuming Web API. With this design, I have to send JsonResult for everything, but, the Http Status Code has to be set appropriately so that the client can use them for any processing that it needs to do based on the Status Code.

First, I set the status code directly in Response in the action method and then returned JsonResult like this:

Response.StatusCode = 400;
return Json(data);
public class BadRequest : JsonResult
{
    public BadRequest()
    {
    }

    public BadRequest(string message)
    {
        this.Data = message;
    }

    public BadRequest(object data)
    {
        this.Data = data;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        context.RequestContext.HttpContext.Response.StatusCode = 400;
        base.ExecuteResult(context);
    }

}
[HttpPost]
public ActionResult Add(Author author, HttpPostedFileBase photoContent)
{
    if (author == null)
    {
        return new BadRequest("Invalid author details.");
    }

    .
    .
    .
}
comments powered by Disqus