This article is a variety of solution thinking processes that are encountered when code optimization (AOP programming for landing validation) is performed on projects in work.
Project background: A simple b/S questionnaire system constructed by Ashx+nvelocity, now needs to optimize the login verification link (after a few months in the review code is a painful process ~)
Nvelocity is the velocity framework's version of. NET, and the core is to return the client after the spelling of the HTML string, which is similar to the front-end code isolation of MVC. In addition to the general handler Ashx does not need to follow the process of generating a control tree like ASP. Therefore the simple system uses the form which the ashx+nvelocity constructs the author individual or the comparison recommendation. If you care so much about the ashx suffix in the access address (such as www.abc.com/news/index.ashx?id=234), you can implement the URL rewrite through the module (HttpModule) entirely.
This article discusses: How to embody the idea of AOP slice programming in ashx?
(1) Reviewing ASP. NET, all pages inherit from the page class, and AOP can be implemented through subclasses of the page. The original is: Default:page, after the slice is inserted: Default:logincheckpage,logincheckpage:page. This makes it possible to write code for login validation in the Logincheckpage class, and to enable the efficient decoupling of all pages that need to be validated-decoupling is relative to writing a Logincheck class and validating on each default page (such as Logincheck.check ( Context) is more conducive to modification and expansion.
(2) The review of MVC, can be leaf out like the above-mentioned, the original is: Homecontroller:controller, after the slice is inserted: Homecontroller:logincheckcontroller, Logincheckcontroller:controller. In addition, AOP can be done using the attribute tags on the class/method header.
There are many other ways to do AOP in ASP, and here, just to think about ashx AOP: Does this approach work in ASHX? Yes! But to do some fine-tuning:
(Write a Logincheck class, and then in each need to verify the ASHX.PR () Add Logincheck.check (context) is not a permanent, so this article does not say otherwise)
The first attempt:(yes, the last time this article tried to succeed, but the process is to write the attempt to take the detour to make a record, and hope to give readers more thoughts of the tips, thanks to insist on reading three kinds of friends to try. )
Use HttpModule. Like URL rewriting, url rewriting is not every request one at a time to do the processing, the module should also be able to do landing it-the difference: ordinary URL rewriting does not involve client isolation, regardless of the requested resource, login authentication to do client isolation (cookie), Consider the requested resource (not all resources are not accessible, there is a visitor level on the line).
For the differences to consider request resources, if you are nauseous, you can write dead in code (can be optimized to Webcofig, other configuration files, database storage) to do the differencing--match the request address with a regular expression to isolate the request to verify the login and the request that does not need authentication.
For client isolation, can I use the session directly in the module? The first thing to do is to inherit the HttpModule from the Ireadonlysessionstate/irequiressessionstate Interface (HttpHandler), So that you can be recognized by. NET when you go through the pipeline. You want to use the session. Register to the BeginRequest event. Don't forget to register to Webconfig. All ready, debugging, error--httpapplication the session property error, the object reference is not set to an object instance. Is it wrong to register the event? I checked the HttpApplication. 19 events in the pipeline, the best entry point between the 第10-11个 event, that is, +=postacquirerequest, in order to get the session, after the execution of ASHX before the login verification.
However, there is no x ... The object reference is still not set to an object instance. Why still not, odd strange.
Another side of a variety of checks, found a good word: module is an application-level thing, is filtering, and the session is a page-level thing, is to be sent according to the request to do different processing, so in the module with the session is not the best solution. So give up module this detour.
Second attempt:
Custom inherits the ashx from IHttpHandler. Original: Index:ihttphandler, optimized after: Index:logincheckhandler,logincheckhandler:ihttphandler. Learn the above method of inserting into an inheritance tree in ASP. But the debug result is not to take the index of the ProcessRequest (), directly after the Logincheckhandler.processrequest () returned, the client is a blank piece. The reason: The general process of implementing IHttpHandler (either index or Logincheckhandler) is only performed once ProcessRequest ().
The third attempt:
On the second basis, change to: ProcessRequest () in Logincheckhandler to virtual and override in the index subclass and call base in Index.processrequest (). ProcessRequest (context). When executing, the program ignores the parent's PR method because it sees override, and the BASE.PR () in the index subclass asks the program to go first to the parent's PR method, and combines the immediate output characteristics of Response.Redirect () (First flush, at end), You can make requests that do not meet the login verification criteria to be blocked out of the door. Xiao Gong Gaocheng!
The trouble is that you want to modify the subclass to override, and there is a base in the subclass. The PR () code (which is also less than a simple, rough call to Logincheck.check (context) to verify that some coupling is reduced), is there a better approach to AOP? Hope you Daniel crossing point.
Using polymorphism to implement AOP (tangent programming) in general handlers (ASHX)