OpenRasta - 将错误编码为 JSON 而不是 HTML
我注意到 OpenRasta.Core 有一个 HtmlErrorCodec,它负责渲染当处理程序抛出异常时发送的服务器错误页面。
当我向异常抛出处理程序发出 JSON Ajax 请求时,会选择此编解码器并将异常呈现为 HTML。
我尝试为 IList
谢谢 尼尔
I notice OpenRasta.Core has an HtmlErrorCodec which is responsible for rendering a the server error page sent out when a handler throws an Exception.
When I make an JSON Ajax request to an exception throwing handler this Codec is selected and the exception is rendered as HTML.
I have tried to register my own IMediaTypeWriter for IList<Error> with MediaType("application/json") so I can send back JSON to the browser, but it seems to be ignored. Can anyone help?
Thanks
Neil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果出现错误,确实会选择带有 IList 的编解码器,但会遵循类型的正常连接。
我建议查看请求日志并找出选择 html 编解码器的方式和原因(我怀疑通过我的远程调试隧道视野,您可能有一个浏览器发送相当于
Accept: text/ html,application/json
,此时 OR 并不真正知道两者中的哪一个是可以接受的,这可能是一个错误,因为我们用 aq 注册 text/html ,而它应该是 0.5)。如果这确实是问题所在,解决方案是删除 html 错误编解码器的注册,您可以通过提供自己的 DependencyRegistrar 来完成此操作。If there is an error, indeed a codec with IList will be selected, but will follow the normal conneg for a type.
I'd suggest having a look at the request log and finding out how and why the html codec gets selected (I'd suspect with my remote debugging tunnel vision that you may have a browser sending the equivalent of
Accept: text/html,application/json
, at which point OR doesn't really know which of the two is acceptable, which is probably a bug as we register text/html with a q of 1 where it should be 0.5). If that's indeed what the problem is, the solution is to remove the registration for the html error codec, which you can do by providing your own DependencyRegistrar.您能否捕获异常,将它们包装在类型中并执行以下操作:
Can you just catch your exceptions, wrap them in a type and do something like: