Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Without this, returning a 400 gives you just "Bad Request" ... with this, you can return a custom response

If you don't have this in your Web.config and your .NET app tries to return a 400 with custom data, you get this ... nomatter what ...

HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 19 Apr 2011 17:47:46 GMT
Content-Length: 11

Bad Request

NOTE You only get this if you're making requests from a REMOTE IP! If you're testing from localhost, you won't see this. You'll see the custom response you want. Don't be fooled! Test your service from a remote machine!

Ok, so ... with this stuff in your Web.config, you'll get what you want:

HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 19 Apr 2011 17:49:02 GMT
Content-Length: 25

{"my":["custom", "json"]}
<?xml version="1.0"?>
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough"></httpErrors>
</system.webServer>
</configuration>
@jondkoon

This comment has been minimized.

Copy link

jondkoon commented Jun 21, 2012

Thank you sir.

@Kwal

This comment has been minimized.

Copy link

Kwal commented Dec 20, 2012

Great find - thanks!

@haneytron

This comment has been minimized.

Copy link

haneytron commented Feb 17, 2014

This was exactly what I was looking for. Thanks.

For those like me who need to understand why this works, I did the research. By default IIS will mask your error codes and replace them with default errors. The "PassThrough" option tells IIS to leave your custom errors alone and render them as-is.

@thiagomajesk

This comment has been minimized.

Copy link

thiagomajesk commented Aug 25, 2015

Man, this just saved my life. Thanks a lot! 😄

@gyosifov

This comment has been minimized.

Copy link

gyosifov commented Jan 19, 2016

Thank you a lot!

@CliffBowman

This comment has been minimized.

Copy link

CliffBowman commented Jun 3, 2016

Worked great - thanks!

@irobinson

This comment has been minimized.

Copy link

irobinson commented Jan 16, 2018

👍

@roimergarcia

This comment has been minimized.

Copy link

roimergarcia commented Feb 3, 2018

Awesome! 👍
You only get this if you're making requests from a REMOTE IP!
So, that's why I can't reproduce locally :)

@UltimaBeaR

This comment has been minimized.

Copy link

UltimaBeaR commented Mar 16, 2018

thanks, it helped

@germanger

This comment has been minimized.

Copy link

germanger commented Jul 10, 2018

Thanks pal.

@MarvinZ

This comment has been minimized.

Copy link

MarvinZ commented Mar 3, 2020

2020 and this still saves jobs!

@iqueryable

This comment has been minimized.

Copy link

iqueryable commented May 12, 2020

Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.