Artem
Artem

Reputation: 1870

How to keep response body when MVC action returns error status?

I want to return BadRequest status and error message from my MVC controller:

public class TestController : System.Web.Mvc.Controller
{
    public ActionResult Bad()
    {
        Response.StatusCode = 400;
        return new JsonResult()
        {
            Data = new { Message = "Request is bad!" },
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }
}

It works when I launch application in IISExpress, the method returns response body:

{"Message":"Request is bad!"}

But when I deploy same site to IIS (ver. 8.5) the response body changed to:

Bad Request

Why this happen? Is there some settings that allows to keep response body when status is not 200?

Upvotes: 0

Views: 735

Answers (1)

Artem
Artem

Reputation: 1870

I found two solutions:

  1. Set Response.TrySkipIisCustomErrors to true.
  2. Set existingResponse to PassThrough in httpErrors

Upvotes: 1

Related Questions