1_b*_*bug 4 asp.net-mvc character-encoding http-status-codes
我需要返回一个带有描述的 HttpStatusCodeResult 对象,其中包含特殊(变音符号)字符。我不知道如何设置正确的编码。
\n\nreturn new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Za\xc5\xbc\xc3\xb3\xc5\x82\xc4\x87 g\xc4\x99si\xc4\x85 ja\xc5\xba\xc5\x84.");\nRun Code Online (Sandbox Code Playgroud)\n\n应该返回:
\n\n\n\n\nZa\xc5\xbc\xc3\xb3\xc5\x82\xc4\x87 g\xc4\x99si\xc4\x85 ja\xc5\xba\xc5\x84。
\n
但我得到:
\n\n\n\n\nZa\xc2\xbf\xc3\xb3\xc2\xb3\xc3\xa6 g\xc3\xaasi\xc2\xb9 ja\xc3\xb1。
\n
我尝试过Response.Charset = System.Text.Encoding.UTF8.WebName;等等,但仍然一无所获。
您不应该得到“Za\xc2\xbf\xc3\xb3\xc2\xb3\xc3\xa6 g\xc3\xaasi\xc2\xb9 ja\xc3\xb1”。任何地方。
\n但你无法得到“Za\xc5\xbc\xc3\xb3\xc5\x82\xc4\x87 g\xc4\x99si\xc4\x85 ja\xc5\xba\xc5\x84”。在 HTTP 状态行原因短语中,因为它仅支持 ASCII 字符集和编码 [ RFC 7230 ]。(HTTP 标头字段确实有 % 编码等机制,但没有状态行。)
\n因此,您可以省略原因短语并将消息放入正文中:
\nResponse.StatusCode = (Int32)HttpStatusCode.BadRequest;\nreturn Content("Za\xc5\xbc\xc3\xb3\xc5\x82\xc4\x87 g\xc4\x99si\xc4\x85 ja\xc5\xba\xc5\x84. And, the quick brown fox jumps over the lazy dog. ");\nRun Code Online (Sandbox Code Playgroud)\n使用您想要的任何内容,包括 HTML。但是,如果内容不够长而显得有用且现代,则接收此内容的 Web 浏览器可能会覆盖该内容。您还应该考虑 Web 服务器是否配置为替换错误页面。这可以帮助:
\n<configuration>\n <system.webServer>\n <httpErrors existingResponse="PassThrough" />\n </system.webServer>\n</configuration>\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1861 次 |
| 最近记录: |