【发布时间】:2019-06-27 21:48:04
【问题描述】:
我为我的 ASP.NET MVC 项目中的某些端点创建了一个自定义 Attribute,它指示服务器返回一个 JSON 对象,而不是按通常的方式处理错误。属性如下所示:
public class AjaxErrorHandler : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
filterContext.HttpContext.Response.StatusDescription =
filterContext.Exception.Message.Replace('\r', ' ').Replace('\n', ' ');
filterContext.Result = new JsonResult
{
Data = new { errorMessage = filterContext.Exception.Message }
};
}
}
}
每当我在本地调试解决方案时,它都能正常工作,并在出错时返回以下内容:
{"errorMessage":"Error message goes here"}
但是当我将解决方案部署到我的生产服务器时,服务器始终返回以下 HTML:
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id = "header" >< h1 > Server Error</h1></div>
<div id = "content" >
< 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>
</div>
</body>
</html>
我这里缺少什么 web 项目的配置,以使服务器遵守返回 JSON 对象的指令?
【问题讨论】:
-
检查本地和生产机器之间的 web.config 设置。您发现某些差异了吗?
-
相同的配置文件。唯一不同的是数据库的连接字符串
标签: asp.net-mvc iis asp.net-mvc-5