gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:23:18 25 4
gpt4 key购买 nike

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

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

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

我想在 ajax 结果中处理页面上的一些错误,但保持通用错误处理。这是允许每个站点自定义 500 条消息的 IIS 设置吗? web.config 自定义错误 On|Off 在我的情况下没有区别。

最佳答案

您对所有异常的过滤器正在检查它是否是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com