如果验证失败,我有一个返回 400 的 Controller 操作:
return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "My custom message");
在本地运行时,我得到标准的“400 Bad Request”,而不是预期的“400 My custom message”。它在部署的站点上运行良好,在我的旧 PC 上本地运行时。
这可能是 IIS 或 Visual Studio 配置问题吗?我试过比较我的旧计算机和当前计算机的 applicationHost.config,但那里没有什么明显的。
回复图片(还不能嵌入):
- 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
编辑:旧计算机的操作系统是 Windows 7,新计算机的操作系统是 Windows 10。两者都安装了 IIS Express 10,该项目配置为使用。
最佳答案
在开发工具中观察请求显示行为不当的请求使用的是 HTTP/2,而所有其他变体(新计算机到部署站点、旧计算机到本地主机、旧计算机到部署站点)都使用 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从旧 PC 转移到新 PC 时突然出现问题:
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.
关于c# - 带有 HTTPStatusCodeResult 的自定义错误消息在本地主机上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46922043/