c# - 带有 HTTPStatusCodeResult 的自定义错误消息在本地主机上不起作用

标签 c# asp.net-mvc http localhost http-status-code-400

如果验证失败,我有一个返回 400 的 Controller 操作:

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

在本地运行时,我得到标准的“400 Bad Request”,而不是预期的“400 My custom message”。它在部署的站点上运行良好,在我的旧 PC 上本地运行时

这可能是 IIS 或 Visual Studio 配置问题吗?我试过比较我的旧计算机和当前计算机的 applicationHost.config,但那里没有什么明显的。

回复图片(还不能嵌入):

编辑:旧计算机的操作系统是 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/

相关文章:

c# - 如何在 C# Windows 窗体中的特定位置定位打开窗体?

c# - 使用 ModelBinder 属性与 ModelBinders.Add()

asp.net-mvc - MVC 路由/RouteDebugger 在部署到 IIS7 时工作,但在 Cassini 中不起作用

c# - 用于 ASP.NET MVC 的 Paypal IPN 监听器

c# - 使用 Linq 的属性选择器和 Where<T> 查询

c# - C# 中名为/exactly do 的值后面的单个字符是什么? (例如1.00m)

c# - 比较两个大小相等的位图以确定它们是否相同的最快方法是什么?

scala - 如何在 Gatling 的 http 请求中获取随机 URL?

http - 如何捕获 WhatsApp 网络客户端通信?

json - 如何在powershell中输出从REST api返回的json对象?