Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Created April 19, 2011 17:50
Show Gist options
  • Select an option

  • Save beccasaurus/929007 to your computer and use it in GitHub Desktop.

Select an option

Save beccasaurus/929007 to your computer and use it in GitHub Desktop.
Without this, returning a 400 gives you just "Bad Request" ... with this, you can return a custom response

If you don't have the XML snippet below in your Web.config and your .NET app tries to return a 400 with custom data, you get this:

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

Copy link
Copy Markdown

Thank you sir.

@kwal

kwal commented Dec 20, 2012

Copy link
Copy Markdown

Great find - thanks!

@haneytron

Copy link
Copy Markdown

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

Copy link
Copy Markdown

Man, this just saved my life. Thanks a lot! ๐Ÿ˜„

@gyosifov

Copy link
Copy Markdown

Thank you a lot!

@CliffBowman

Copy link
Copy Markdown

Worked great - thanks!

@irobinson

Copy link
Copy Markdown

๐Ÿ‘

@roimergarcia

Copy link
Copy Markdown

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

@UltimaBeaR

Copy link
Copy Markdown

thanks, it helped

@germanger

Copy link
Copy Markdown

Thanks pal.

@MarvinZ

MarvinZ commented Mar 3, 2020

Copy link
Copy Markdown

2020 and this still saves jobs!

@iqueryable

Copy link
Copy Markdown

Thanks ๐Ÿ‘

@astamatov

Copy link
Copy Markdown

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

in8Sukka commented Feb 9, 2021

Copy link
Copy Markdown

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

@markashleybell

Copy link
Copy Markdown

Thank you! This was super useful.

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