gpt4 book ai didi

asp.net-mvc - ASP.NET MVC、HandleError 属性和跟踪 : Bug?

转载 作者:行者123 更新时间:2023-12-01 19:12:10 28 4
gpt4 key购买 nike

在 ASP.NET MVC 应用程序中启用跟踪时,我似乎遇到了一些奇怪的行为:

每当启用跟踪时,HandleError 属性都会失败。

我在普通的 ASP.NET MVC 应用程序上重现了这个,并且想知道是否有人经历过类似的事情。

重现步骤

步骤 1

创建新的 ASP.NET MVC 应用程序

步骤 2

web.config 中打开跟踪:

<trace enabled="true" localOnly="false" pageOutput="false" requestLimit="500" traceMode="SortByTime" />

步骤 3

此时一切正常。主页加载:

http://localhost/MvcApplication2/

并且跟踪页面有效:

http://localhost/mvcapplication2/trace.axd

步骤 4

HandleError 属性可以找到它的地方模拟异常( Controller 操作、 View )。

我在 Home\Index.aspx View 中抛出异常:

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

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
<% throw new NotImplementedException(); %>
</p>
</asp:Content>

结果

我没有获取 HandleError 过滤器 (Shared\Error.aspx) 返回的 View ,而是收到了 ASP.NET CustomErrors 错误:

http://localhost/mvcapplication2/GenericErrorPage.htm?aspxerrorpath=/MvcApplication2/

深入挖掘

步骤 5

web.config 中禁用跟踪:

<!--<trace enabled="true" localOnly="false" pageOutput="false" requestLimit="500" traceMode="SortByTime" />-->

结果

HandleError 过滤器 View (Shared\Error.aspx) 已正确返回:

Sorry, an error occurred while processing your request.

观察结果

返回第 4 步并进行一些调试,结果显示:

  • HandleError 被正确调用
  • HandleError View (Shared\Error.aspx) 引发错误,这就是我们被发送到 ASP.NET 自定义错误的原因

感谢 ELMAH,这是 View 抛出的异常:

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Web.HttpException: Multiple controls with the same ID 'ctl00' were found. Trace requires that controls have unique IDs.
at System.Web.TraceContext.AddNewControl(String id, String parentId, String type, Int32 viewStateSize, Int32 controlStateSize)
at System.Web.UI.Control.BuildProfileTree(String parentId, Boolean calcViewState)
at System.Web.UI.Control.BuildProfileTree(String parentId, Boolean calcViewState)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
--- End of inner exception stack trace ---
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at ASP.views_error_internalerror_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\c3a94d6e\b487cfcc\App_Web_m5awwxof.0.cs:line 0
at System.Web.Mvc.ViewPage.RenderView(ViewContext viewContext)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

(请注意,对于此跟踪,我的 HandleError View 称为“InternalError.aspx”,而不是默认的“Error.aspx”)

错误?

所以问题是,我会因为发现错误而得到一 block cookie,还是因为错过一些明显的东西而被鳟鱼打?

预先感谢您的帮助!

最佳答案

作为后续:

问题仍然存在,但同时我已经实现了解决方法。

我定义了一个自定义 HandleError 属性,因为我正在插入 ELMAH:

如何让 ELMAH 与 ASP.NET MVC [HandleError] 属性一起使用? How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?

OnException 方法的末尾,我简单地添加了以下内容:

filterContext.HttpContext.Trace.IsEnabled = false;

通过以编程方式禁用对该请求的跟踪,我能够避免硬崩溃。

由于无论如何都会记录异常,因此我也不会因为关闭跟踪而遭受损失。

关于asp.net-mvc - ASP.NET MVC、HandleError 属性和跟踪 : Bug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1152198/

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