How is ASP. NET httpHandler used? Let's take a look at the discussion: first, we will analyze the process of ASP. NET processing a web request. Learning ASP. NET technology, in addition to reading people's documents, this is the most basic, knowledge is knowledge, do not understand the basic knowledge can not be learned in depth), you should also learn to ponder, hands-on practice.
When a web request is sent from the client to the web server, it is processed by the web server or transferred to the asp.net framework for processing. If no web server uses asp.net's default web processing), you can refer to CASSINI source code analysis> Implementation of your own ASP. NET host system.
ASP. NET httpHandler usage question: assuming that the request is sent to the ASP. NET Framework, how does the system return?
The Asp.net system framework itself has an http processing logic. This logic is specified by the httpRuntime section of machine. config.
- ﹤httpHandlers﹥。。。﹤/httpHandlers﹥
Tells the http handler of an application in the ASP. NET Framework how to determine the processing logic by default. How to handle the request is determined by the combination of http predicate Verb) and resource location Path.
For example:
- ﹤add
-
- verb="*"
-
- path="*.aspx"
-
- type="System.Web.UI.PageHandlerFactory" /﹥
Tell ASP. NET to process any post/get/head/put requests for any ". aspx" files. Use the System. Web. UI. PageHandlerFactory class to process requests. This class is the default class of the System framework, which follows the System. Web. IhttpHandler interface but is officially implemented by MS ). If you are studying the aspx page framework, you can write An aspx page factory processing program by yourself, and add the signature to the global application program, then modify the settings so that when the aspx page is not processed, you can perform the operations based on your own implementation logic, even without following the page control implementation logic, though that doesn't make much sense ).
Let's make an experiment: we use a browser to send a request to the. config file, such as web. config), but asp.net reports an error while saving the time, but if we delete
- ﹤add
-
- verb="*"
-
- path="*.config"
-
- type="System.Web.HttpForbiddenHandler" /﹥
And save. After you request the file again, the browser can read the file and display it. If you add this section again, the system reports an error. This fully demonstrates that the http processing factory processes the data according to the configuration file. Accordingly, we can read other related configuration sections of machine. config to fully understand how the default processing logic of the system processes http requests. In addition, web. config can also add/delete specified http handlers accordingly. By referring to MSDN, we found that any HTTP handler actually implements the asp.net class of the System. Web. IhttpHandler interface. The interface should implement an attribute and an excuse method:
Public attributes
IsReusable gets a value indicating whether other requests can use the IHttpHandler instance. Determines whether the processing program can be reused. It usually means that the system performance is improved)
Common Methods
ProcessRequest enables HTTP Web request processing by implementing the custom HttpHandler interface of the IHttpHandler interface. That is, the processing implementation.
ASP. NET httpHandler usage question: Suppose we have written an http processing program, how can we make it play a role? For example, if we need to disable asp.net from downloading the. info file, we should do the following:
Step 1: Add instructions on the web server for asp.net to process the extension. Specifically:
Open IIS (assuming the web server is IIS), find the application, configuration, application ing, add ing, and enter the current in the executable file.. net version aspnet_isapi.dll (for example, C: \ WINNT \ Microsoft. NET \ Framework \ v1.0.3705 \ aspnet_isapi.dll), extension input. info, all predicates, check whether the file exists. After these steps, make sure that IIS does not handle the. info file on its own, instead, it is sent to aspnet_isapi.dll, which then sends the request to asp.net for processing.
Step 2: add the ing configuration section to machine. config or web. config. All are added to the
- ﹤httphandlers﹥
-
- …
-
- ﹤add verb=”*” path=”*.info” type=” System.Web.HttpForbiddenHandler”﹥
-
- …
-
- ﹤/httphandlers﹥
After the configuration in the preceding section, the. info file will be protected, and any attempt to access the file will be notified that "this type of page cannot be provided ."; If you change to your own class, it is called by the asp.net framework to process. info requests.
The following is an example of implementing an http processing program. We will add an extension. the img and imgfiles generate an image based on parameters.. NET Framework request abc. img, then the system returns an image in image/jpg format, and the image content is abc
The source code is as follows:
- UsingSystem;
-
- UsingSystem. Web;
-
- UsingSystem. Drawing;
-
- UsingSystem. Drawing. Drawing2D;
-
- UsingSystem. Drawing. Imaging;
-
-
- NamespaceImyWeb
-
- {
- Public ClassIMG: System. Web. IHttpHandler
-
- {
-
- PublicIMG ()
-
- {}
-
- Public BoolIsReusable
-
- {
-
- Get{Return True;}
- }
- Public VoidProcessRequest (HttpContext context)
-
- {
-
- StringVstr = _ getViewString (context );
-
- Context. Response. ContentType ="Image/jpeg";
-
- Image img =NewBitmap (128,128, PixelFormat. Format32bppArgb );
-
- Graphics g = Graphics. FromImage (img );
-
- Brush backBrush =NewSolidBrush (Color. Gray );// Gray
-
- Brush textBrush =NewSolidBrush (Color. Black );
-
- G. FillRectangle (backBrush, 128,128 );
-
- Font ft =NewFont ("Arial", 32 );
-
- G. DrawString (vstr, ft, textBrush,
- NewRectangleF (0, 0, 128,128 ),
- NewStringFormat (StringFormatFlags. NoWrap ));
-
- Img. Save (context. Response. OutputStream, ImageFormat. Jpeg );
-
- Context. Response. Flush ();
-
- BackBrush. Dispose ();
-
- TextBrush. Dispose ();
-
- G. Dispose ();
-
- Img. Dispose ();
-
- Return;
-
- }
-
- //************//
-
- Private String_ GetViewString (HttpContext context)
-
- {
-
- StringStr = context. Request. RawUrl;
-
- IntL1 = str. LastIndexOf ("/");
-
- IntL2 = str. LastIndexOf (".");
-
- ReturnStr. Substring (l1 + 1, l2-l1-1 );
-
- }
-
- }
-
- }
After compilation, the application is myHttpHandler. dll.
Now, we need to tell the ASP. NET application how to handle the. imgfile, assuming that our web application is under localHost/webApp1:
Step 1: Add instructions on the web server so that asp.net can process the. img extension. Let IIS rest. do not intervene in asp.net and let asp.net process *. img)
Second, add the following content in the <system. web> section of web. config:
- ﹤httpHndlers﹥
-
- ﹤add verb="*" path="*.img" type="IBuySpy.IMG,IMGHttphandler" /﹥
-
- ﹤/httpHndlers﹥
To enable the ASP. NET program to locate the application assembly, copy myHttpHandler. dll to the bin directory of the web application.
Next, let's test: in the tested web application WebApp1, request a. img resource at will and a jpg image will be obtained. Http: // localhost/WebApp1/test. img
The above is a simple ASP. NET httpHandler processing program design and installation process. In fact, assume that you use the default * For asp.net *. Aspx HttpHandler is not enough to handle. You can design it by yourself and overwrite the default processing class *. aspx in machine. config "System. Web. UI. PageHandlerFactory ". You need to understand that ms has designed a service framework with basic processing capabilities, however, the best part of this framework is that it can be replaced based on your own needs. We can see a large number of interfaces used, we should realize that it is using a lot of modern software design technologies and ideas ). To learn about the asp.net technology, we need to figure out how to execute, call, relationship, and county roles among all components of the framework. This is more meaningful than implementing a fancy datagrid.
Here is a simple example of ASP. NET httpHandler. I hope you can understand the basic usage of ASP. NET httpHandler.
- Introduction to ASP. NET Custom Controls
- ASP. NET Server Control View
- Detailed explanation of the lifecycle of ASP. NET components
- Analysis on Transmission Mechanism of ASP. NET Component Design
- Analysis on complex attributes and status management of ASP. NET Component Design