2014-02-17 12 views

Antwort

5

filterContext.ExceptionHandled wird auf true gesetzt, wenn eine Ausnahme von einer Aktionsmethode ausgelöst wird. In der Standardeinstellung wurde HandleErrorAttribute in FilterConfig Klasse hinzugefügt, die in Application_Start() registriert ist. Wenn eine Ausnahme auftritt, wird die OnException-Methode in HandleErrorAttribute Klasse aufgerufen.

In der Methode OnException wird die ExceptionHandled-Eigenschaft vor dem Entfernen des aktuellen HTTP-Antworthauptteils mithilfe von Response.Clear() auf true festgelegt.

Unten ist die Standard OnException Methode:

public virtual void OnException(ExceptionContext filterContext) 
{ 
    if (filterContext == null) 
    { 
     throw new ArgumentNullException("filterContext"); 
    } 
    if (filterContext.IsChildAction) 
    { 
     return; 
    } 
    if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled) 
    { 
     return; 
    } 
    Exception exception = filterContext.Exception; 
    if (new HttpException(null, exception).GetHttpCode() != 500) 
    { 
     return; 
    } 
    if (!ExceptionType.IsInstanceOfType(exception)) 
    { 
     return; 
    } 
    string controllerName = (string)filterContext.RouteData.Values["controller"]; 
    string actionName = (string)filterContext.RouteData.Values["action"]; 
    HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName); 

    filterContext.Result = new ViewResult 
    { 
     ViewName = View, 
     MasterName = Master, 
     ViewData = new ViewDataDictionary<HandleErrorInfo>(model), 
     TempData = filterContext.Controller.TempData 
    }; 
    filterContext.ExceptionHandled = true; 
    filterContext.HttpContext.Response.Clear(); 
    filterContext.HttpContext.Response.StatusCode = 500; 
    filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; 
} 
+3

Haben Sie eine Quelle für „filterContext.ExceptionHandled auf true gesetzt werden, wenn eine Ausnahme durch eine Aktion-Methode ausgelöst wird“? – heymega