ASP.NET MVC HandleError 不起作用(customErrors 设置为“On”)

发布于 2024-07-24 05:18:05 字数 1367 浏览 10 评论 0原文

我相信我已经将 MVC 应用程序设置为正确使用 [HandleError]。 这是我的控制器:

[HandleError]
public class SupportController : BaseController {

    public ActionResult Toss() {
        throw new Exception("uh oh");
    }
    // snip
}

并且我web.config中的customErrors标记设置为“On”:

<customErrors mode="On"></customErrors>

但是,我仍然收到黄色异常时出现死亡屏幕。 在我的 Toss() 操作中设置断点显示 HttpContext.IsCustomErrorEnabled is 设置为 true。

我们没有对视图引擎做任何事情,并且 BaseController 也不会搞乱任何事情(其他不扩展它的控制器也有同样的问题)。

我正在 Windows XP 上进行开发,当应用程序部署到服务器 2003 盒子 (IIS 6) 时遇到同样的问题。

我认为 error.aspx 页面上没有异常:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="errorContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Sorry, an error occurred while processing your request.
    </h2>
</asp:Content>

可能不同的一件事是,这个应用程序是在 MVC Beta 是最新版本时创建的,并升级到了 RC 和然后 RTM 发布。 可能有一些奇怪的设置是从那里遗留下来的吗?

我可以在其他应用程序上使用它,所以我有点困惑。

I believe I have set up our MVC app to use [HandleError] properly. This is my controller:

[HandleError]
public class SupportController : BaseController {

    public ActionResult Toss() {
        throw new Exception("uh oh");
    }
    // snip
}

And I have set my customErrors tag in web.config to "On":

<customErrors mode="On"></customErrors>

However, I am still getting the Yellow Screen of Death on Exceptions. Setting a breakpoint in my Toss() action shows that HttpContext.IsCustomErrorEnabled is set to true.

We are not doing anything with the View Engine, and the BaseController doesn't mess with anything either (and other Controllers that do not extend it have the same issue).

I am developing on Windows XP, and have the same issue when the app is deployed to a server 2003 box (IIS 6).

I do not think there is an Exception on the error.aspx page:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="errorContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Sorry, an error occurred while processing your request.
    </h2>
</asp:Content>

One thing that may be different is that this app was created back when MVC Beta was the latest version, and was upgraded to the RC and then RTM as they were released. Might there have been some kooky setting that is left over from there?

I can get this working on other apps, so I'm a little flummoxed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

掐死时间 2024-07-31 05:18:05

如果处理错误页面时出现问题,这种情况往往会发生。如果您调试应用程序,在初始异常之后,您将遇到另一个异常(错误页面的异常)。我遇到了这种情况以及我的原因是因为我有一个强类型母版页,错误页面正在使用此母版页,并且因为母版页与实际页面共享相同的模型,所以母版页正在获取 HandlerErrorInfo 模型,而不是我期望的类型化模型。

就我个人而言,我认为这是 asp.net mvc 中的一个糟糕的设计(以及它的其余部分),但是您可以通过不使用相同的母版页来轻松解决这个问题(您甚至可以在继承了母版页的情况下进行母版页继承)强类型母版页纯粹从非类型化母版页继承该层。

否则,这是错误视图中发生的某种异常。(最有可能)。

This tends to happen if there is a problem processing the error page.. if you debug the app, right after the initial exception you'll problem hit another (exception from the error page).. I had this happening and the reason for me was because I had a strongly typed master page, the error page was using this master page, and because the masterpage shares the same model as the actual page, the master page was getting a HandlerErrorInfo model, instead of the typed model I expected..

Personally I think this is a poor design in the asp.net mvc (along with the rest of it), but you can get around this easy enough by not using the same master page (you could even do masterpage inheritance where you have an inherited strongly typed master page that purely inherits the layer from the untyped one..

Otherwise this is some sort of exception happening in the error view.. (most likely).

梦毁影碎の 2024-07-31 05:18:05

如果您使用的是 IE9,则会出现另一个问题。 如果错误页面小于 1Kb,IE9 似乎会隐藏错误页面,如:

asp-net-mvc-3-handleerror-global-filter-always-shows-iis-status-500-page

确保内容正文长度超过 1Kb 。
尝试在错误页面末尾添加以下内容:

@(new String(' ', 1000))

If you are using IE9 there is another issue. It seems IE9 hide the error page if it is less than 1Kb, as said in:

asp-net-mvc-3-handleerror-global-filter-always-shows-iis-status-500-page

Be sure content body is more than 1Kb length.
Try adding this at the end of the error page:

@(new String(' ', 1000))

沙与沫 2024-07-31 05:18:05

这个问题的另一个原因可能是,

在模板MVC应用程序(由VS2008 / VS2008 Express生成)中,Error.aspx(由VS生成)使用母版页。

如果母版页访问任何 ViewData ,它将抛出空引用异常,然后 error.aspx 将不会显示。

使用这个简单的代码作为您的 Error.aspx ,它将解决问题,(与 CustomErrors=On 一起)

<%@ Page Language="C#"  Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
<%= Model.Exception.Message %>

Another reason for this problem may be ,

In Template MVC Application (generated by VS2008 / VS2008 Express) , Error.aspx (generated by VS) uses Master Page.

If Master Page access any ViewData it will throw null reference Exception , then the error.aspx won't be shown.

Use this Simple code as your Error.aspx , it will solve the problem, (along with CustomErrors=On )

<%@ Page Language="C#"  Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
<%= Model.Exception.Message %>
維他命╮ 2024-07-31 05:18:05

我一直遇到类似的问题。 我通过这个“hack”得到了理想的结果。

由于我的所有控制器都扩展了 BaseController,因此这在任何地方都适用。

public class BaseController : Controller
{
    protected override void OnException(ExceptionContext filterContext)
    {

        Response.Write("<pre>" + filterContext.Exception.StackTrace + "</pre>");

        base.OnException(filterContext);

    }

}

I kept running into a similar problem. I was able to get a desirable result with this "hack".

Since all my controllers extend a BaseController, this works everywhere.

public class BaseController : Controller
{
    protected override void OnException(ExceptionContext filterContext)
    {

        Response.Write("<pre>" + filterContext.Exception.StackTrace + "</pre>");

        base.OnException(filterContext);

    }

}
知足的幸福 2024-07-31 05:18:05

我遇到了同样的问题,经过调查,我发现我正在更新 web.config 下的 web.Debug.config 文件。

然后我在 web.config 文件中创建了 CustomErrors 标记,它对我有用。

I had a same problem, and after investigating, I found that I was updating the web.Debug.config file under web.config.

Then I created the the CustomErrors tag in the web.config file and it was working for me.

家住魔仙堡 2024-07-31 05:18:05

我最近遇到了类似的问题。 事实证明,抛出异常的控制器(HomeController)中的方法正在被另一个控制器(DispatchController)中的方法调用。

我们必须将 [HandleError] 属性放置在调用控制器上才能使其正常工作。

[HandleError(ExceptionType = typeof(SessionExpireException), View = "ErrorSessionExpired")]
public class DispatchController : Controller
{

我们还将自定义错误视图放置在调用控制器的文件夹结构中。
查看调度控制器文件夹中显示自定义错误视图的文件夹

I ran into a similar issue recently. It turned out that the method in the controller that throws the exception (HomeController) was being called by a method in a different controller (DispatchController).

We had to place the [HandleError] property on the calling controller to get it to work.

[HandleError(ExceptionType = typeof(SessionExpireException), View = "ErrorSessionExpired")]
public class DispatchController : Controller
{

We also placed the custom error view in the folder structure for the calling controller.
View folder showing custom error view in the Dispatch controller folder

撩心不撩汉 2024-07-31 05:18:05

经过一番折腾后,我发现我的问题属于“从代码中抛出异常只是为了在渲染错误视图时抛出另一个错误”的一般类别。 我之前编写了一个强类型错误页面,期望出现 System.Exception,然后当我切换到使用 [HandleError] 属性时,过滤器向错误页面传递 System.HandleErrorInfo。 第二个异常发生在这一行(在 Default.aspx.cs 中):

httpHandler.ProcessRequest(HttpContext.Current);

令人困惑。

After much hair-pulling, I found that my problem falls into the general category of "throwing exception from code only to throw another error when rendering the error view." I had written a strongly-typed error page earlier expecting a System.Exception, then when I switched to using the [HandleError] attribute, the filter was passing the error page a System.HandleErrorInfo. The second exception happened at this line (in Default.aspx.cs):

httpHandler.ProcessRequest(HttpContext.Current);

Confusing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文