在ASP.NET MVC应用程序中启用跟踪时,我似乎遇到了一些时髦的行为:
每当启用跟踪时,HandleError属性都会失败。
我在vanilla ASP.NET MVC应用程序上重现了这个,并且想知道是否有人经历过类似的事情。
创建一个新的ASP.NET MVC应用程序
启用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/
在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”)
所以问题是,我是否会找到一个用于查找错误的cookie,或者是否因为遗漏某些明显的东西而被sla sla?
提前感谢您的帮助!
答案 0 :(得分:0)
作为后续行动:
问题仍然存在,但同时我实施了一种解决方法。
我定义了自定义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;
通过以编程方式禁用对该请求的跟踪,我能够避免硬崩溃。
由于异常被记录,我也不会因为关闭跟踪而失败。