ASP.NET MVC3中的HTML反而不是JSON的IIS响应

主题是自我介绍的.我有开发人员和生产环境.开发人员环境是我的本地机器.我有一些行为方法,在某些事情出错(或发生错误或逻辑不一致))时,将响应状态代码设置为500,并返回Json-answer.我的常见方法如下:

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

在客户端生产环境当发生这样的错误时,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>

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

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

解决方法

我想IIS正在服务一些友好的错误页面.您可以尝试通过在响应上设置TrySkipIisCustomErrors属性来跳过此页面:

catch(Exception ex)
{
     Response.StatusCode = 500;
     Response.TrySkipIisCustomErrors = true;
     return Json(new { message = "error" },JsonBehaviour.AllowGet)
}

dawei

【声明】:淮南站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。