小编kok*_*sda的帖子

在ASP .NET MVC3上使用HTML而不是JSON进行IIS响应

该主题是自我解释的.我有开发人员和生产环境.开发者环境 是我的localhost机器.我在contolers中有动作方法,当出现错误(出错或逻辑不一致)并返回Json-answer时,将响应状态代码设置为500.我的常用方法如下:

[HttpPost]
public ActionResult DoSomething(int id)
{
    try
    {
         // some useful code
    }
    catch(Exception ex)
    {
         Response.StatusCode = 500;
         Json(new { message = "error" }, JsonBehaviour.AllowGet)
    }
}
Run Code Online (Sandbox Code Playgroud)

在客户端生产环境.当发生这样的错误时,ajax.response看起来像一个HTML代码,而不是预期的JSON.

考虑一下:

<div class="content-container">
 <fieldset>
   <h2>500 - Internal server error.</h2>
   <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
</fieldset>
</div>
Run Code Online (Sandbox Code Playgroud)

过滤器上下文不是一个选项.我认为这是某种IIS或web.config问题.

解决方案: 我们决定TrySkipIisCustomErrors在Global.asax中添加BeginRequest,它解决了我们应用程序中每个方法的问题.

error-handling json asp.net-mvc-3

12
推荐指数
1
解决办法
3487
查看次数

标签 统计

asp.net-mvc-3 ×1

error-handling ×1

json ×1