Returning a body content with 400 HTTP status code

7 Feb
2011

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.

[HttpPost]
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

<system.webServer>

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

</system.webServer>

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.

rest_endpoint_xml
existingResponse=”PassThrough”

rest_endpoint_NOxml
existingResponse=”Replace”

For more information, go to MSDN web site. https://msdn.microsoft.com/en-us/library/ms690497(VS.90).html"p-more">



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

Avatar

Heath

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.

Avatar

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.

Avatar

Ryan

May 12th, 2011 at 5:37 pm

Would setting Response.TrySkipIisCustomErrors = true have the same effect? https://www.west-wind.com/weblog/posts/2009/Apr/29/IIS-7-Error-Pages-taking-over-500-Errors

Avatar

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.

Avatar

Daniel

July 1st, 2011 at 11:59 pm

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

Avatar

Om

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.

Avatar

Cengiz Han

January 13th, 2012 at 6:56 am

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

Avatar

cengizhan2forum

January 16th, 2012 at 7:19 pm

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

Avatar

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.

Avatar

Jindal Saha

May 15th, 2012 at 11:51 am

You saved me today, Thanks a lot

Comment Form

top