Returning a body content with 400 HTTP status code

7 Feb

If you are working in a RESTful project or an ajax based web application probably you will need to return http error codes with custom content. Like returning a custom XML message from your endpoint with a specific (like 400 BadRequest) HTTP Status Code.

public ActionResult Create(Customer customer)

    var validationResult = _validator.Validate(customer);

    if (validationResult.HasErrors)
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return CreateXmlContentActionResult(validationResult.ToXml());

    //do magic here

    return CreateXmlContentActionResult(CustomerCreateSuccesMessage);

protected ActionResult CreateXmlContentActionResult(string xmlData)
    return Content(xmlData, "text/xml");

IIS 7 has an option named keeping existing response content. This option specifies what happens to an existing response when the HTTP status code is an error. For enabling custom content for http error status codes you need to change your web.config file. Under system.webServer element


    <httpErrors existingResponse="PassThrough"></httpErrors>


Auto: Leaves the response untouched only if the SetStatus flag is set.
Replace: Replaces the existing response even if the SetStatus flag is set.
PassThrough: Leaves the response untouched if an existing response exists.

Check out same endpoint request from fiddler screenshots. Only different between two calls is existingResponse option. Second screenshot doesn’t return custom xml error message but first one does because it’s existingResponse value is PassThrough.



For more information, go to MSDN web site."p-more">

10 Responses to Returning a body content with 400 HTTP status code



February 8th, 2011 at 7:51 pm

I think you should have a tag with your name at the top, bottom or both of each post. I was wondering who wrote this post a first.


Cengiz Han

February 8th, 2011 at 10:28 pm

I hope this means you liked to read this. I will add a author name section at the end of entry. thank you for feedback.



May 12th, 2011 at 5:37 pm

Would setting Response.TrySkipIisCustomErrors = true have the same effect?


Cengiz Han

May 13th, 2011 at 5:08 am

thanks for feedback. that’s another way to do that. Actually, I didn’t know. Thanks.



July 1st, 2011 at 11:59 pm

I would have wasted hours trying to figure this out… Thanks for posting!



December 23rd, 2011 at 7:16 pm

It doesn’t matter what we try we can’t duplicate your solution for custom 400 error responses. For us, HTTP.sys always takes priority and responds with the default Bad Request page. We’re using IIS 7 on Windows 2008 sp1. It’s an ASP.NET 4 application using MVC 3. Are you able to share a test project or offer any suggestions? Thanks.


Cengiz Han

January 13th, 2012 at 6:56 am

try changing applicationHost.config file like below (instead of Deny set Allow)



January 16th, 2012 at 7:19 pm

güzel bir makale olmuş çok teşekkürler.


Ozgur Yogurtcu

February 3rd, 2012 at 4:05 am

Thanks I lost hours before I figured out why returned data is different in prod and dev environments.


Jindal Saha

May 15th, 2012 at 11:51 am

You saved me today, Thanks a lot

Comment Form