asp.net MVC integration EntLib Implementation of "Automation" exception handling [implementation Article]

Source: Internet
Author: User
Tags exception handling execution

Through the actual demo of the example, we can see that the automatic exception handling mechanism that we implemented through extension can use EntLib Ehab to process the exception that is thrown during the execution of an action method according to a processing policy. For the processed results, the request is responded to in the following mechanism. [Source code download from here]

For AJAX requests, directly create a data object to encapsulate the handled exception, and create a jsonresult to reply the exception information to the client.

For non-AJAX requests, if the Handleerroractionattribute attribute is applied on the current action method and the matching action method is set to handle the exception thrown by the method, then the method is executed and returned The ActionResult object back responds to the current request.

If the Handleerroractionattribute attribute is not applied to the current action method, or the action specified through the attribute does not exist, the default error view is rendered as a response to multiple requests.

First, Exceptionpolicyattribute & Handleerroractionattribute

All of this is done through a custom exceptionfilter. But instead of defining any of the exceptionfilter features, we implement the exception handling in a custom Extendedcontroller base class, where the automatic processing of the exception is implemented in the overridden Onexception method, However, before introducing the logic of the method, let's take a look at the other auxiliary members defined in Extendedcontroller.

1:public class Extendedcontroller:controller
2: {
3:private static Dictionary<type, controllerdescriptor> controllerdescriptors = new Dictionary<type, control Lerdescriptor> ();
4:private static Object synchelper = new Object ();
5:
6:protected override void Onexception (Exceptioncontext filtercontext)
7: {
8://ellipsis Member
9:}
10:
11://Describe the current controller controllerdescriptor
12:public Controllerdescriptor Descriptor
13: {
14:get
15: {
16:controllerdescriptor descriptor;
17:if (this. controllerdescriptors.trygetvalue GetType (), out descriptor)
18: {
19:return descriptor;
20:}
21:lock (Synchelper)
22: {
23:if (this. controllerdescriptors.trygetvalue GetType (), out descriptor)
24: {
25:return descriptor;
26:}
27:else
28: {
29:descriptor = new Reflectedcontrollerdescriptor (this. GetType ());
30:controllerdescriptors.add (this. GetType (), descriptor);
31:return descriptor;
32:}
33:}
34:}
35:}
36://Get exception handling policy name
37:public string Getexceptionpolicyname ()
38: {
39:string actionname = ControllerContext.RouteData.GetRequiredString ("action");
40:actiondescriptor Actiondescriptor = this. Descriptor.findaction (ControllerContext, actionname);
41:if (Null = = Actiondescriptor)
42: {
43:return string. Empty;
44:}
45:exceptionpolicyattribute Exceptionpolicyattribute = Actiondescriptor.getcustomattributes (True). Oftype<exceptionpolicyattribute> (). FirstOrDefault ()??
46:descriptor.getcustomattributes (True). Oftype<exceptionpolicyattribute> (). FirstOrDefault ()?? New Exceptionpolicyattribute ("");
47:return Exceptionpolicyattribute.exceptionpolicyname;
48:}
49:
50://Get Handle-error-action Name
51:public string Gethandleerroractionname ()
52: {
53:string actionname = ControllerContext.RouteData.GetRequiredString ("action");
54:actiondescriptor Actiondescriptor = this. Descriptor.findaction (ControllerContext, actionname);
55:if (Null = = Actiondescriptor)
56: {
57:return string. Empty;
58:}
59:handleerroractionattribute Handleerroractionattribute = Actiondescriptor.getcustomattributes (True). Oftype60:descriptor.getcustomattributes (True). Oftype61:return handleerroractionattribute.handleerroraction;
62:}
63:
64://For execution of Handle-error-action Actioninvoker
65:public Handleerroractioninvoker Handleerroractioninvoker {get; private set;}
66:
67:public Extendedcontroller ()
68: {
69:this. Handleerroractioninvoker = new Handleerroractioninvoker ();
70:}
71:}

Related Article

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.