Asp.net mvc 按照说明,IIS返回默认错误HTML而不是JSON
我在ASP.NET MVC项目中为某些端点创建了一个自定义的Asp.net mvc 按照说明,IIS返回默认错误HTML而不是JSON,asp.net-mvc,iis,asp.net-mvc-5,Asp.net Mvc,Iis,Asp.net Mvc 5,我在ASP.NET MVC项目中为某些端点创建了一个自定义的属性,它指示服务器返回一个JSON对象,而不是以通常的方式处理错误。该属性如下所示: public class AjaxErrorHandler : FilterAttribute, IExceptionFilter { public void OnException(ExceptionContext filterContext) { if (filterContext.HttpContext.Reque
属性
,它指示服务器返回一个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>
#内容{边距:02%;位置:相对;}
.content容器{背景:#FFF;宽度:96%;边距顶部:8px;填充:10px;位置:相对;}
-->
服务器错误
500-内部服务器错误。
您正在查找的资源有问题,无法显示。
为了让服务器遵守返回JSON对象的指令,我在这里缺少了什么web项目配置?我以前在IIS中见过这种情况
从内存中,您可以尝试以下操作:
- 打开IIS管理器
- 选择有问题的网站
- 双击错误页面图标
- 单击右侧的“编辑要素设置”
- 将本地错误页和远程请求的自定义错误页的设置更改为详细错误
- 请重试该站点
我想这就是我过去解决这个问题的方法检查本地机器和生产机器之间的web.config设置。您发现了某些差异吗?相同的配置文件。唯一的区别是到数据库的连接字符串