asp.net mvc 3 handleerror 全局过滤器始终显示 IIS 状态 500 页 [英] asp.net mvc 3 handleerror global filter always shows IIS status 500 page

查看:16
本文介绍了asp.net mvc 3 handleerror 全局过滤器始终显示 IIS 状态 500 页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了一切,甚至卸载了 asp.net mvc3,但我无法让 HandleError 全局过滤器工作.

I have tried everything, even uninstalling asp.net mvc3, and I can't get HandleError global filter working.

我在 Global.asax 中设置了 HandleError 过滤器:

I have set up the HandleError filter in the Global.asax:

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
 {
     filters.Add(new HandleErrorAttribute());
 }

此外,我启用了 CustomErrors(是否设置 defaultRedirect="Error" 并不重要,我认为这是在文档中,因为旧版本的 mvc 需要):

Also I have CustomErrors enabled (it does not matter if i set defaultRedirect="Error" or not, I think that is in the docs because is needed for older versions of mvc):

<customErrors mode="On" />

尝试浏览页面直到出现错误,无论您是从 localhost 还是使用主机名,在开发服务器或 IIS 7.5 中,它总是重定向到标准状态 500 页面,而不是我的自定义 Error.cshtml我在共享中创建的视图.这是错误视图代码:

Trying to navigate through the page until the error gets raised, wether you do from localhost or using the hostname, inside the development server or IIS 7.5, it always redirects to a standard status 500 page, instead of my custom Error.cshtml view that I have created in Shared. Here is Error view code:

@model System.Web.Mvc.HandleErrorInfo

@{
    ViewBag.Title = "Oooops";
}

<h2>Ooops Something really bad happened!</h2>

我还注意到,如果我创建一个新的 ASP.NET MVC3 项目,然后选择Internet 应用程序"模板,并在该项目中启用 customErrors,那么 HandleError 过滤器开始工作得很好,但是使用空的 MVC3 模板没有.

Also I have noted that if I create a new ASP.NET MVC3 project and then select "Internet Application" template, and just enabling customErrors in that project, then the HandleError filter starts working just fine, however using the empty MVC3 template does not.

我想澄清一下,我确实可以在调试时看到正在处理的错误视图,但是浏览器总是显示错误 500 页面.

I want to clarify, that indeed I can see the error view being processing when debugging, however the browser always display Error 500 page.

推荐答案

我遇到了同样的问题.我找到了这篇文章: ASP.NET MVC 中的 500 个内部服务器错误 并尝试取消选中友好的 Http 错误",我的 HandleErrors 属性开始在 IE 8 中按预期工作,并检查了 IE 7 兼容模式(并开始像在 Chrome 中一样默认工作).我不认为这对于已部署的应用程序来说是一个有效的解决方案,但也许有一种方法可以扩展 HandleError 属性,以便它重定向到错误"控制器,而不仅仅是默认的错误视图.

I ran into this same issue. I found this post: 500 Internal Server Error in ASP.NET MVC and tried unchecking "Friendly Http Errors" and my HandleErrors attribute started working as expected in IE 8 with IE 7 compatibility mode checked (and started working like it does in Chrome by default). I don't think this is a valid solution for a deployed app, but perhaps there's a way to extend the HandleError attribute so that it redirects to an "Error" controller instead of just the default Error view.

更新:这篇文章表明当返回 HTTP 状态 500 时,IE 会处理它尴尬地.绕过它的一种方法是将状态设置为 200,然后一切似乎都正常(即使在 IE 中).

UPDATE: This post shows that when an HTTP Status 500 is returned, IE handles it awkwardly. A way to get around it, is to set the Status to 200, then everything seems to work OK (even in IE).

public class CustomHandleErrorAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {    
        base.OnException(filterContext);

        //https://stackoverflow.com/questions/5137627/mvc-handleerror-returns-error-500-and-view-error-page
        //https://stackoverflow.com/questions/183316/asp-net-mvc-handleerror
        filterContext.HttpContext.Response.StatusCode = 200;
    }
}

这篇关于asp.net mvc 3 handleerror 全局过滤器始终显示 IIS 状态 500 页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆