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

Source: Internet
Author: User

Personally feel that exception handling for programmers is the most familiar and the most difficult to master. Say it's familiar, because it's just try/catch/finally. It's difficult to grasp, because many developers are not sure where try/catch/finally should be located? Under what circumstances should the exception be logged? Under what circumstances should an exception be encapsulated? Under what circumstances do I need to replace the exception? Under what circumstances will it be necessary to throw the caught exception again? Under what circumstances is it not necessary?

Reasonable exception handling should be scene driven, in different scenarios, the exception handling strategy is often different. The strategy of exception handling should be configurable, because what happens to an application is often unpredictable, and the inadequacy of existing exception strategies often needs to be reflected when something really happens, so we need a dynamically configurable way to maintain the exception handling strategy. At present, some open source exception handling framework provides this configurable, scene-driven exception handling, EntLib Exception Handling Application Block (hereinafter referred to as Ehab) is a good choice. [Source code download from here]

First, respond to requests by specifying handle-error-action

Before we get into the formal introduction of how to implement and EntLib to automate exception handling, let's take a look at the "Automation" features of exception handling. As an example of a user login scenario, we defined a simple data type in a Web application created through the ASP.net MVC project template in Visual Studio Logininfo encapsulates the user name and password required for user login.

   1:public class Logininfo
2: {
3: [DisplayName ("username")]
4: [Required (errormessage= "Please enter {0}")]
5: Public string UserName {get; set;}
6:
7: [DisplayName ("password")]
8: [Required (errormessage = "Please enter {0}")]
9: [DataType (Datatype.password)]
Public string Password {get; set;}
11:}

Then we define a homecontroller as follows. The Http-get action Method index will present a user login view that uses the created Logininfo object as its model. The true user authentication logic is defined in another index method that has the Httppostattrubute attribute applied: If the user name is not foo, throw a invalidusernameexception exception, or if the password is not "password", Throws the invalidpasswordexception exception. Invalidusernameexception and Invalidpasswordexception are two of our custom exception types.

 1: [Exceptionpolicy ("Defaultpolicy")] 
2:public class Homecontroller:extendedcontroller
3: {
4:public actionresult Index ()
5: {
6:return View (New Logininfo ());
7:}
8: 
9: [HttpPost]
: [Handleerroraction ("Onindexerror")]
11:public actio Nresult Index (Logininfo logininfo)
: {
13:if (string. Compare (Logininfo.username, "foo", True)!= 0)
: {
15:throw new invalidusernameexception ( );
16:}
17: 
18:if (logininfo.password!= "Password")
: {
20:throw new I Nvalidpasswordexception ();
21:}
22:return View (logininfo);
23:}
24: 
: [HttpPost]
26:public actionresult onindexerror (logininfo logininfo)
27: { br> 28:return View (logininfo);
29:}
:}

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.