Custom error message with HTTPStatusCodeResult not working on localhost

一曲冷凌霜 提交于 2019-12-11 06:06:39

问题


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):

  • The response from localhost on the new machine
  • The response from the deployed site on the new machine
  • The response from localhost on the old machine

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.


回答1:


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.



来源:https://stackoverflow.com/questions/46922043/custom-error-message-with-httpstatuscoderesult-not-working-on-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!