views:

41

answers:

2

I want my ASP.NET MVC application return HTTP response with code 400 and description of the error as body. My code is:

response.StatusCode = 400;
response.Write("Some error");
response.End();

When I test this code on localhost, I get expected result. But on remote server, response text contains just text description of the error, in this case "Bad Request".

  1. Could you please explain this behavior?
  2. What is solution to this?

Web Server - IIS7, locahost means that I access from local computer, remote - from remote.

A: 

When you say "localhost" you mean the Cassini development web server used when debugging in VS right?

The server you are deploying to is IIS6? By default it traps 400 and sends its own response. I'm not sure there is a way you can prevent it from doing that (you might be able to muck about with the MetaBase but I'm not sure). You can specify a URL within your site of a page you want executed when this code is sent.

That said why are you using 400 and not simply throwing an exception? 400 is a little abused by protocols like SOAP which extends the meaning of a badly formed HTTP request up into the application but that's border line since SOAP is also a transfer envelope.

However it seems odd to me to have an application that has accepted a request to then send back a 400 response, you would normally send a variation on 500 instead.

AnthonyWJones
Sorry, I havent specified web-server, for both local and remote I use IIS7.
Mike Chaliy
+1  A: 

Answer is simple:

response.TrySkipIisCustomErrors = true;
Mike Chaliy
This is only for IIS 7+, though.
Craig Stuntz
Yes, this is one annoying bugger.
Igor Zevaka