Application_Error 中的重定向在 IE 和 Edge 中不起作用
Redirect in Application_Error not working in IE & Edge
我已经全局编写了一些代码以在出现未处理的错误时重定向到自定义 MVC 操作。
此代码在 Chrome 中运行良好,但在 IE 和 Edge 中运行失败。 IE 和 Edge 正在显示它们的默认错误页面。
这是我的 global.asax 的 Application_Error
protected void Application_Error(object sender, EventArgs e)
{
var lastException = Server.GetLastError();
var wrappedContext = new HttpContextWrapper(Context);
wrappedContext.Response.StatusCode = (lastException as HttpException)?.GetHttpCode() ?? 500;
wrappedContext.Response.Clear();
wrappedContext.Response.TrySkipIisCustomErrors = true;
wrappedContext.Server.ClearError();
var routeData = new RouteData();
routeData.Values.Add("area", "");
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
routeData.Values.Add("httpResponseCode", Context.Response.StatusCode);
IController errorController = DependencyResolver.Current.GetService<ErrorController>();
errorController.Execute(new RequestContext(wrappedContext, routeData));
}
上面的代码工作正常...问题出在视图中...
代码如下:
@{
ViewBag.Title = "Oops, something went wrong...";
Layout = null;
}
TODO: Nice error page
IE 和 Edge 显示自定义错误页面有以下限制 (Table source):
所以添加一些超过 512 字节的虚拟文本使其工作...
不知道为什么 MS 在 IE 中这样做,为什么在 Edge 中更是如此?!?
我已经全局编写了一些代码以在出现未处理的错误时重定向到自定义 MVC 操作。
此代码在 Chrome 中运行良好,但在 IE 和 Edge 中运行失败。 IE 和 Edge 正在显示它们的默认错误页面。
这是我的 global.asax 的 Application_Error
protected void Application_Error(object sender, EventArgs e)
{
var lastException = Server.GetLastError();
var wrappedContext = new HttpContextWrapper(Context);
wrappedContext.Response.StatusCode = (lastException as HttpException)?.GetHttpCode() ?? 500;
wrappedContext.Response.Clear();
wrappedContext.Response.TrySkipIisCustomErrors = true;
wrappedContext.Server.ClearError();
var routeData = new RouteData();
routeData.Values.Add("area", "");
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
routeData.Values.Add("httpResponseCode", Context.Response.StatusCode);
IController errorController = DependencyResolver.Current.GetService<ErrorController>();
errorController.Execute(new RequestContext(wrappedContext, routeData));
}
上面的代码工作正常...问题出在视图中...
代码如下:
@{
ViewBag.Title = "Oops, something went wrong...";
Layout = null;
}
TODO: Nice error page
IE 和 Edge 显示自定义错误页面有以下限制 (Table source):
所以添加一些超过 512 字节的虚拟文本使其工作...
不知道为什么 MS 在 IE 中这样做,为什么在 Edge 中更是如此?!?