在启用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" />
第三步
此时一切正常。主页已经加载:
http://localhost/MvcApplication2/
并且跟踪页面可以正常工作:
http://localhost/mvcapplication2/trace.axd
第四步
在HandleError
属性可以找到的某个地方(控制器操作、视图)模拟异常。
我在Home\Index.aspx
视图中抛出了一个异常:
<%@ 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
)返回的视图,我收到了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
过滤器视图 (Shared\Error.aspx
) 被正确返回:
抱歉,处理您的请求时出现错误。
观察结果
回到第4步并进行一些调试后发现:
HandleError
被正确调用HandleError
视图 (Shared\Error.aspx
) 抛出了一个错误,这就是我们被发送到 ASP.NET 自定义错误页面的原因
多亏了 ELMAH,以下是视图抛出的异常:
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
视图被称为“InternalError.aspx”,而不是默认的“Error.aspx”。
有Bug吗?
所以问题是,我是否因为发现了一个bug而得到了一个饼干,还是因为错过了一些显而易见的事情而被打了一条鱼?
提前感谢您的帮助!