Ich habe hoch und niedrig gesucht und ich kann keine klare Antwort finden. Wenn ich ein benutzerdefiniertes Attribut/Filter habe, wird die Methode OnActionExecuted
immer aufgerufen? Auch wenn eine Ausnahme ausgelöst wird?Wird die OnActionExecuted-Methode eines Attributs immer ausgeführt?
10
A
Antwort
3
Zumindest mit MVC 5 ist @ tvanfossons Antwort nicht mehr korrekt. Dies kann auch für frühere Versionen gelten.
OnActionExecuted
wird immer aufgerufen und hat Zugriff auf die ausgelöste Ausnahme über filterContext.Exception
.
Testfall mit Ausnahme in einer Aktion:
public class HomeController
: Controller
{
public ActionResult Index()
{
throw new Exception("Index");
}
}
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new Foo());
filters.Add(new Bar());
}
}
public class Foo
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine($"{nameof(Foo)}.{nameof(OnActionExecuting)}");
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine($"{nameof(Foo)}.{nameof(OnActionExecuted)}");
Debug.WriteLine($"Has exception: {filterContext.Exception != null}");
base.OnActionExecuted(filterContext);
}
}
public class Bar
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine($"{nameof(Bar)}.{nameof(OnActionExecuting)}");
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine($"{nameof(Bar)}.{nameof(OnActionExecuted)}");
Debug.WriteLine($"Has exception: {filterContext.Exception != null}");
base.OnActionExecuted(filterContext);
}
}
Output:
Bar.OnActionExecuting
Foo.OnActionExecuting
Exception thrown: 'System.Exception' in WebApplication1.dll
Foo.OnActionExecuted
Has exception: True
Bar.OnActionExecuted
Has exception: True
Testfall mit Ausnahme in einem Filter
public class HomeController
: Controller
{
public ActionResult Index()
{
return new HttpStatusCodeResult(200);
}
}
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new Foo());
filters.Add(new Bar());
}
}
public class Foo
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine($"{nameof(Foo)}.{nameof(OnActionExecuting)}");
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine($"{nameof(Foo)}.{nameof(OnActionExecuted)}");
Debug.WriteLine($"Has exception: {filterContext.Exception != null}");
throw new Exception("Foo");
base.OnActionExecuted(filterContext);
}
}
public class Bar
: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Debug.WriteLine($"{nameof(Bar)}.{nameof(OnActionExecuting)}");
base.OnActionExecuting(filterContext);
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
Debug.WriteLine($"{nameof(Bar)}.{nameof(OnActionExecuted)}");
Debug.WriteLine($"Has exception: {filterContext.Exception != null}");
base.OnActionExecuted(filterContext);
}
}
Output:
Bar.OnActionExecuting
Foo.OnActionExecuting
Foo.OnActionExecuted
Has exception: False
Exception thrown: 'System.Exception' in WebApplication1.dll
Bar.OnActionExecuted
Has exception: True
Verwandte Themen
- 1. Wird immer mit IISNode ausgeführt
- 2. Wie erstellt man eine Task, die immer ausgeführt wird?
- 3. Steuern der Existenz eines Attributs
- 4. AsyncTask wird immer ausgeführt, selbst wenn App zerstört wird?
- 5. Hinzufügen eines benutzerdefinierten Attributs zu json.net
- 6. Die "action" und "onsubmit" eines Formulars: Welches wird zuerst ausgeführt?
- 7. Abbrechen eines Prozesses, während es ausgeführt wird
- 8. Wie wird das Python-Fenster als "Immer im Vordergrund" ausgeführt?
- 9. In Express.js warum wird Code nach res.json() immer noch ausgeführt?
- 10. Gradle: Force Custom Task wird immer ausgeführt (kein Cache)
- 11. Xcode 7.1 stürzt immer ab, wenn das Debugging ausgeführt wird
- 12. Lösung Bereitstellung von Sharepoint ausgeführt wird für immer - Sharepoint 2007
- 13. Berechnung der Entropie eines bestimmten Attributs?
- 14. Ändern eines Bild-Src-Attributs mit jQuery nicht immer in Chrome/Opera angewendet
- 15. didReceiveRemoteNotification wird nicht ausgeführt, wenn App ausgeführt wird (FOREGROUND)
- 16. Get Parameter eines Attributs mit Reflection
- 17. Auswählen eines Attributs aus dem angeklickten href
- 18. Erweitern eines Attributs durch Hinzufügen einer Eigenschaft
- 19. Wie erhalten Sie die Optionen eines konfigurierbaren Attributs in Magento?
- 20. Wie finde ich die Zieleigenschaft eines UIHInt-Attributs?
- 21. BroadcastReceiver, wenn die Anwendung nicht ausgeführt wird
- 22. Winkeleinstellung eines ausgewählten Attributs mit ng-options
- 23. Einschränkung des Eigenschaftstyps eines benutzerdefinierten Attributs
- 24. Magento - Hinzufügen eines benutzerdefinierten Attributs zur Bestellung
- 25. Erste eine Teil eines Attributs in XPATH
- 26. Wie Wert eines Attributs erhalten, indem HtmlAgilityPack
- 27. Hinzufügen eines benutzerdefinierten Attributs mit Magento-Shop
- 28. XPath-Format ein Datum eines Attributs
- 29. Warum werden die Meteor Router-Funktionen immer zweimal ausgeführt?
- 30. Wert eines Attributs im DetailView-Widget ändern
Danke für die Aktualisierung dieses für MVC 5! – Patricia