ajax - MVC 通用错误 View 和 Ajax Post 和错误代码 500

标签 ajax asp.net-mvc iis razor

我已经设置了一个带有 _error.cshtml 的 mvc 应用程序,该应用程序设置为捕获我在 Controller 中抛出的异常。

我还在某些页面上发布了一些 ajax 帖子,用于检查错误,然后执行其他操作。

在服务器上,我对所有异常进行过滤,然后检查它是否是ajax请求并返回可以在客户端上反序列化的内容。问题是,如果我不将发布响应状态代码设置为 500,那么 ajax 将不会看到此错误,并且我无法显示一条好的消息。如果我将状态设置为 500,我会收到默认的 IIS 错误消息,说明服务器上发生了某些情况。

我想处理ajax结果中页面上的一些错误,但保持通用错误处理。这是允许每个站点自定义 500 条消息的 IIS 设置吗? web.config 自定义错误开|关对我来说没有什么区别。

最佳答案

您对所有异常进行的过滤器正在检查是否是 ajax 请求,这是您自己制作的过滤器吗?

我遇到了一个稍微类似的问题,我必须确保标记 TrySkipIisCustomErrors 设置为 true 以避免标准 IIS 错误。 该标志位于 HttpContext 的 Response 对象上。

这也是由标准的 HandleError 过滤器完成的,请注意其 OnException 方法实现中的最后一行:

    public virtual void OnException(ExceptionContext filterContext) {
        if (filterContext == null) {
            throw new ArgumentNullException("filterContext");
        }
        if (filterContext.IsChildAction) {
            return;
        }

        // If custom errors are disabled, we need to let the normal ASP.NET exception handler
        // execute so that the user can see useful debugging information.
        if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled) {
            return;
        }

        Exception exception = filterContext.Exception;

        // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
        // ignore it.
        if (new HttpException(null, exception).GetHttpCode() != 500) {
            return;
        }

        if (!ExceptionType.IsInstanceOfType(exception)) {
            return;
        }

        string controllerName = (string)filterContext.RouteData.Values["controller"];
        string actionName = (string)filterContext.RouteData.Values["action"];
        HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
        filterContext.Result = new ViewResult {
            ViewName = View,
            MasterName = Master,
            ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
            TempData = filterContext.Controller.TempData
        };
        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = 500;

        // Certain versions of IIS will sometimes use their own error page when
        // they detect a server error. Setting this property indicates that we
        // want it to try to render ASP.NET MVC's error page instead.
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    }

关于ajax - MVC 通用错误 View 和 Ajax Post 和错误代码 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13479775/

相关文章:

javascript - 使用 Jquery 访问 innerhtml

jquery - 每5秒刷新一次表数据

从 2.2 迁移到 3.1 的 ASP.Net Core 解决方案在发布时不会运行

Jquery getJSON 无法跨站点工作

javascript - 使网站横幅幻灯片占据整个宽度

javascript - 从 Razor 输出 JavaScript

asp.net - 在 asp.net 5 MVC 6 中使用 Linq to SQL

c# - 默认文档回发失败

apache - 将域名指向Tomcat

javascript - 将对象和文件从 ajax 非法调用传递到 Web Api 2 Controller