Integrating EntLib to implement ASP. net mvc Exception Handling

Source: Internet
Author: User

 

This article uses custom ASP. net mvc Exception filters implement integration with EntLib's EHAB (Exception Handling Application Block), so that we can define Exception Handling policies through configuration, the error page displays the handled exception information. [Download the source code from here]

 

We know that ASP. net mvc has an exception filter of HandleErrorAttribute type, which can be used to guide the error page. Here, I directly let our custom exception filter inherit from it. Therefore, we define the type, for example, ExtendedHandleErrorAttribute. We create ExtendedHandleErrorAttribute by specifying the configuration name of the exception handling policy, while the property ExceptionPolicy indicates the specific exception handling policy. In the override OnException method, we call the ExceptionPolicyImpl HandleException method in try/catch, and the object passed in this method is the exception to be processed. The caught exception may be the original exception or the handled exception, which depends on the postHandlingAction settings.

1: public class ExceptionHandlingAttribute: HandleErrorAttribute

2 :{

3: public ExceptionPolicyImpl ExceptionPolicy {get; private set ;}

4:

5: public exceptionhandlingattriname (string exceptionPolicyName)

6 :{

7: this. ExceptionPolicy = policiselibrarycontainer. Current. GetInstance <ExceptionPolicyImpl> (exceptionPolicyName );

8 :}

9:

10: public override void OnException (ExceptionContext filterContext)

11 :{

12: try

13 :{

14: this. ExceptionPolicy. HandleException (filterContext. Exception );

15 :}

16: catch (Exception ex)

17 :{

18: filterContext. Exception = ex;

19: base. OnException (filterContext );

20 :}

21 :}

22 :}

Next, we will define the View that displays the error message. In the Views \ Shared directory, create an Error. cshtml file of the Model type HandleErrorInfo. The following is the content of the entire file. We can see that the exception message, type, and stack tracing information are displayed.

1: @ model System. Web. Mvc. HandleErrorInfo

2 :@{

3: ViewBag. Title = "Error ";

4 :}

5:

6: <p> <B> Exception Type: </B> @ this. Model. Exception. GetType () </p>

7: <p> <B> StackTrace: </B> @ this. Model. Exception. StackTrace </p>

Then, for example, the next controller named HomeController. In the Action method Index, we execute an integer division operation with the DivideByZeroException to make it throw a DivideByZeroException. The custom exception filter is directly applied to the HomeController type, and the Exception Handling Policy Name is set to UI Policy. The View attribute is set to the View name created above for displaying error information.

1: [ExtendedHandleError ("UI Policy", View = "Error")]

2: public class HomeController: Controller

3 :{

4: public ActionResult Index ()

5 :{

6: int x = 1;

7: int y = 0;

8: int result = x/y;

9: return View ();

10 :}

11 :}

Finally, let's look at the defined Web. for the DivideByZeroException thrown in config, we replace it with the CalculationErrorException exception and specify the "Calculation Error…" exception message after replacement ...". As for the PostHandlingAction attribute, it is set to ThrowNewException, which means that the handled exception will be thrown out. In our example, the replaced CalculationErrorException will be thrown.

1: <configuration>

2: <configSections>

3: <section name = "exceptionHandling"

4: type = "Microsoft. Practices. EnterpriseLibrary. ExceptionHandling. Configuration. ExceptionHandlingSettings, Microsoft. Practices. EnterpriseLibrary. ExceptionHandling"/>

5: </configSections>

6 :...

7: <exceptionHandling>

8: <exceptionPolicies>

9: <add name = "UI Policy">

10: <exceptionTypes>

11: <add name = "InvalidOperationException"

12: type = "System. DivideByZeroException, mscorlib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"

13: postHandlingAction = "ThrowNewException">

14: <exceptionHandlers>

15: <add name = "ReplaceHandler"

16: type = "Microsoft. Practices. EnterpriseLibrary. ExceptionHandling. ReplaceHandler, Microsoft. Practices. Exceptions. ExceptionHandling, Version = 5.0.414.0, Culture = neutral, PublicKeyToken = token"

17: replaceExceptionType = "Artech. Web. Mvc. Extensions. CalculationException, EhabIntegration"

18: predictionmessage = "Calculation Error..."/>

19: </exceptionHandlers>

20: </add>

21: </exceptionTypes>

22: </add>

23: </exceptionPolicies>

24: </exceptionHandling>

25: </configuration>

Now let's run our program. Because HomeController and Index are the default controllers and actions, they will direct to the error page and display the exception information after replacement.

 

 

Author: Artech

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.