Record the garbage log on line the view ' Error ' or its master is not found

Source: Internet
Author: User

Recently monitored online logs, the website was developed by ASP. NET MVC, and many error logs were found to record the same content:

The view ' Error ' or its master is not found or no view engine supports the searched locations. The following locations were searched:~/views/search/error.aspx~/views/search/error.ascx~/views/shared/error.aspx~ /views/shared/error.ascx~/views/search/error.cshtml~/views/search/error.vbhtml~/views/shared/error.cshtml~/ Views/shared/error.vbhtmlsystem.web.mvc at   System.Web.Mvc.ViewResult.FindView (controllercontext context)

Local debugging will not be found, through the discovery of local and online configuration differences in CustomErrors mode= "on", the local after the single-point debug discovery is really the error record, and itself I throw out in action the exception has not been crawled. That is, the real exception is not crawled, and the record is a trivial and inexplicable log, because I do not have any code to return to the "Error" view.

First of all, the current way I crawl exceptions: Add in the Global.asax file

        protected void Application_Error (object  sender, EventArgs e)        {            = server.getlasterror ();             // Write Log        }

If the controller inside the override Onexception method to write the log will not have such a problem, but in the controller log may make the error in the view of the log records.

Bored for a long time, and finally inadvertently found that MVC will default to register a filter, the code is as follows

         Public Static void registerglobalfilters (globalfiltercollection filters)        {            filters. ADD (new  Handleerrorattribute ());        }

Registration can solve the above problems, but I am not convinced, so in CodePlex over the MVC source code, Handleerrorattribute code as follows:

usingSystem.Diagnostics.CodeAnalysis;usingSystem.Globalization;usingSystem.Web.Mvc.Properties;namespacesystem.web.mvc{[SuppressMessage ("microsoft.performance","ca1813:avoidunsealedattributes", justification ="This attribute is AllowMultiple = True and the users might want to override behavior.")] [AttributeUsage (AttributeTargets.Class| AttributeTargets.Method, inherited =true, AllowMultiple =true)]     Public classHandleerrorattribute:filterattribute, Iexceptionfilter {Private Const stringDefaultView ="Error"; Private ReadOnly Object_typeid =New Object(); PrivateType _exceptiontype =typeof(Exception); Private string_master; Private string_view;  PublicType Exceptiontype {Get{return_exceptiontype;} Set            {                if(Value = =NULL)                {                    Throw NewArgumentNullException ("value"); }                if(!typeof(Exception). IsAssignableFrom (value)) {Throw NewArgumentException (String.Format (CultureInfo.CurrentCulture, Mvcresources.exceptionviewattribute_nonexceptiontype, value.                FullName)); } _exceptiontype=value; }        }         Public stringMaster {Get{return_master??String.Empty;} Set{_master =value;} }         Public Override ObjectTypeId {Get{return_typeid;} }         Public stringView {Get{return(! String.IsNullOrEmpty (_view))?_view:defaultview;} Set{_view =value;} }         Public Virtual voidonexception (Exceptioncontext filtercontext) {if(Filtercontext = =NULL)            {                Throw NewArgumentNullException ("Filtercontext"); }            if(filtercontext.ischildaction) {return; }            //If Custom Errors is disabled, we need to let the normal ASP. Exception Handler//Execute so, the user can see useful debugging information.            if(filtercontext.exceptionhandled | |!filterContext.HttpContext.IsCustomErrorEnabled) {return; } Exception Exception=filtercontext.exception; //If this is not an HTTP $ (for example, if somebody throws an HTTP 404 from an action method),//ignore it.            if(NewHttpException (NULL, exception). Gethttpcode ()! = -)            {                return; }            if(!Exceptiontype.isinstanceoftype (Exception)) {                return; }            stringControllername = (string) filtercontext.routedata.values["Controller"]; stringActionName = (string) filtercontext.routedata.values["Action"]; Handleerrorinfo Model=NewHandleerrorinfo (filtercontext.exception, Controllername, ActionName); Filtercontext.result=NewViewResult {ViewName=View, Mastername=Master, ViewData=NewViewdatadictionary(model), TempData=FilterContext.Controller.TempData}; Filtercontext.exceptionhandled=true;            FilterContext.HttpContext.Response.Clear (); FilterContext.HttpContext.Response.StatusCode= -; //Certain versions of IIS would sometimes use their own error page when//they detect a server error. Setting This property indicates that we//want it to a try to render ASP. NET MVC ' s error page instead.FilterContext.HttpContext.Response.TrySkipIisCustomErrors =true; }    }}
Handleerrorattribute

Dad, he's in MVC. The error view is returned by default, and it looks like an empty project will be created later in the new development.

As for why the customerrors mode= "on" will not appear after this garbage log, you have to blame the Handleerrorattribute class Onexception method inside

        // If Custom Errors is disabled, we need to let the normal ASP. Exception            Handler // Execute so, the user can see useful debugging information.            if (filtercontext.exceptionhandled | |! filterContext.HttpContext.IsCustomErrorEnabled)            {                return;            }

The solution is to delete the default registered filter or add class to override the corresponding method.

Record the garbage log on line the view ' Error ' or its master is not found

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.