The following two methods are used to handle asp net mvc errors:
Method 1: Mark the HandleError attribute on the controller or action and specify an error page. This method is the easiest. You do not need to add additional actions. You only need to add error pages, but cannot record logs (because there is no action, you can also call the log recording method in aspx ). This error page can also be defined as HandleErrorInfo, which is of the strong type. The specific Model and framework are passed to obtain the specific exception information.
HandlError
/// <Summary>
/// Mark HandleError and specify the error handling page as AboutError. aspx
/// </Summary>
/// <Returns> </returns>
[HandleError (View = "AboutError")]
Public ActionResult About ()
{
Return View ();
}
AboutError. aspx
<% @ Page Title = "" Language = "C #" MasterPageFile = "~ /Views/Shared/Site. Master "Inherits =" System. Web. Mvc. ViewPage <HandleErrorInfo> "%>
<Asp: Content ID = "Content1" ContentPlaceHolderID = "TitleContent" runat = "server">
AboutError
</Asp: Content>
<Asp: Content ID = "Content2" ContentPlaceHolderID = "MainContent" runat = "server">
Attackers can obtain exception information by using a strong type.
<% = Model. Exception. Message %>
</Asp: Content>
This method is flexible. For example, you can use this method to define an error page for an action. However, there will be a small problem, which will be mentioned later.
Method 2: override the onException method of the controller class. This method is the most direct and usually used in the BaseController of a project. Later, the controller can inherit this class, you can record error logs in the cs code, but you need to define the action and specific error page of the error page. You must set ExceptionHandled = true in the method. Otherwise, the error will be thrown to the outer layer. In this case, you can only handle the error through the traditional aspnet method. This is usually the error page configured in CustomerError, if no configuration is available, a yellow page is displayed. Predictionhandled = true is a subtle logical change.
Override OnException
Protected override void OnException (ExceptionContext filterContext)
{
// Mark the exception that has been handled
FilterContext. ExceptionHandled = true;
// Jump to the error page
FilterContext. Result = new RedirectResult (Url. Action ("Error", "Shared "));
}
The two methods are discussed mainly to illustrate the problems that may occur when these two methods are mixed. If the HandleError attribute is marked with action, and the controller in the same period overwrites the OnException method, what will be done in the end? According to the execution order of the filter in mvc, the method rewritten by the controller will be executed first, regardless of the order in the action. After the execution is complete, the filter method marked by the action will be executed. OK. With this theory, let's look at the execution sequence of the situations mentioned above. First, execute the processing method in OnException. At this time, filterContext. predictionhandled has been marked as true. When the HandleError attribute method is executed, it will not be executed. That is to say, the custom error page is in vain and does not work. This is because the built-in HandleError will first judge filterContext during execution. if predictionhandled is true or not, it will not be executed if it is true. Therefore, there may be some strange bugs. If you understand this, you will know how to handle it.
However, filterContext cannot always be used. exceptionHandled = true; this line of code is removed because other actions do not mark the handle error attribute. If filterContext is not enabled. if ExceptionHandled is true, the error will still be thrown to the outer layer and handed over to mermererror for processing. Therefore, to maintain the OnException method of the base class and customize the error pages of the action, you must define the ExceptionFilter yourself instead of using the built-in method. Method 3 is required.
Method 3: Customize the ExceptionFilter. You need to inherit the FilterAttribute and implement the IExceptionFilter interface. You do not need to determine whether the Exception has been processed in the implementation. However, you must note that the action AboutError is required.
Custom predictionfilter
Public class AboutErrorAttribute: FilterAttribute, IExceptionFilter
{
# Region IExceptionFilter Member
Public void OnException (ExceptionContext filterContext)
{
UrlHelper url = new UrlHelper (filterContext. RequestContext );
FilterContext. Result = new RedirectResult (url. Action ("AboutError", "AboutError "));
}
# Endregion
}
In this case, you need to change
View Code
/// <Summary>
/// Mark the custom AboutError
/// </Summary>
/// <Returns> </returns>
[AboutError]
Public ActionResult About ()
{
Return View ();
}
After a round of tossing, the demand was finally realized, that is, the general exception was handled properly, and the personalized error pages of some actions were also implemented, which was relatively neat. However, the HandleError of the best framework can provide the property settings that ignore and determine whether an exception has been processed.