NicholasC
NicholasC

Reputation: 56

Custom error message with HTTPStatusCodeResult not working on localhost

I have a controller action that returns a 400 if validation fails:

return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "My custom message");

When running locally I get the standard "400 Bad Request", instead of the expected "400 My custom message". It works fine on the deployed site, and when running locally on my old PC.

Could this be an IIS or Visual Studio configuration problem? I've tried diffing the applicationHost.config of my old and current computers but there was nothing obvious there.

Images of the responses (can't embed yet):

Edit: Old computer's OS was Windows 7, new computer's OS is Windows 10. Both have IIS Express 10 installed, which the project is configured to use.

Upvotes: 2

Views: 2243

Answers (1)

NicholasC
NicholasC

Reputation: 56

Watching the requests in dev tools revealed that the misbehaving request is using HTTP/2, and all the other variants (new computer to deployed site, old computer to localhost, old computer to deployed site) are using HTTP/1.1.

From the HTTP/2 specification:

HTTP/2 does not define a way to carry the version or reason phrase that is included in an HTTP/1.1 status line.

And the reason it suddenly became a problem when moving from the old PC to the new one:

HTTP/2 is a rework of how HTTP semantics flow over TCP connections, and HTTP/2 support is present in Windows 10 and Windows Server 2016.

Upvotes: 2

Related Questions