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 jondkoon commented Jun 21, 2012

Thank you sir.

@Kwal

This comment has been minimized.

Copy link

@Kwal Kwal commented Dec 20, 2012

Great find - thanks!

@haneytron

This comment has been minimized.

Copy link

@haneytron 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 thiagomajesk commented Aug 25, 2015

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

@gyosifov

This comment has been minimized.

Copy link

@gyosifov gyosifov commented Jan 19, 2016

Thank you a lot!

@CliffBowman

This comment has been minimized.

Copy link

@CliffBowman CliffBowman commented Jun 3, 2016

Worked great - thanks!

@irobinson

This comment has been minimized.

Copy link

@irobinson irobinson commented Jan 16, 2018

👍

@roimergarcia

This comment has been minimized.

Copy link

@roimergarcia 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 UltimaBeaR commented Mar 16, 2018

thanks, it helped

@germanger

This comment has been minimized.

Copy link

@germanger germanger commented Jul 10, 2018

Thanks pal.

@MarvinZ

This comment has been minimized.

Copy link

@MarvinZ MarvinZ commented Mar 3, 2020

2020 and this still saves jobs!

@iqueryable

This comment has been minimized.

Copy link

@iqueryable iqueryable commented May 12, 2020

Thanks 👍

@astamatov

This comment has been minimized.

Copy link

@astamatov astamatov commented Sep 18, 2020

This works great for returning json, but this will cause problem with 404 redirection - it would not work. Do you have a clue how to have both working?

@in8Sukka

This comment has been minimized.

Copy link

@in8Sukka in8Sukka commented Feb 9, 2021

What if i am getting this error in an Azure function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment