Analysis of ASP. NET httpHandler

Source: Internet
Author: User

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.

 
 
  1. ﹤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:

 
 
  1. ﹤add  
  2.  
  3.    verb="*" 
  4.  
  5.   path="*.aspx" 
  6.  
  7.   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

 
 
  1. ﹤add  
  2.  
  3.     verb="*" 
  4.  
  5.     path="*.config" 
  6.  
  7.    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

 
 
  1. ﹤httphandlers﹥  
  2.  
  3.  …  
  4.  
  5.  ﹤add verb=”*” path=”*.info” type=” System.Web.HttpForbiddenHandler”﹥  
  6.  
  7. …  
  8.  
  9. ﹤/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:

 
 
  1. UsingSystem;
  2.  
  3. UsingSystem. Web;
  4.  
  5. UsingSystem. Drawing;
  6.  
  7. UsingSystem. Drawing. Drawing2D;
  8.  
  9. UsingSystem. Drawing. Imaging;
  10.  
  11.  
  12. NamespaceImyWeb
  13.  
  14. {
  15. Public ClassIMG: System. Web. IHttpHandler
  16.  
  17. {
  18.  
  19. PublicIMG ()
  20.  
  21. {}
  22.  
  23. Public BoolIsReusable
  24.  
  25. {
  26.  
  27. Get{Return True;}
  28. }
  29. Public VoidProcessRequest (HttpContext context)
  30.  
  31. {
  32.  
  33. StringVstr = _ getViewString (context );
  34.  
  35. Context. Response. ContentType ="Image/jpeg";
  36.  
  37. Image img =NewBitmap (128,128, PixelFormat. Format32bppArgb );
  38.  
  39. Graphics g = Graphics. FromImage (img );
  40.  
  41. Brush backBrush =NewSolidBrush (Color. Gray );// Gray 
  42.  
  43. Brush textBrush =NewSolidBrush (Color. Black );
  44.  
  45. G. FillRectangle (backBrush, 128,128 );
  46.  
  47. Font ft =NewFont ("Arial", 32 );
  48.  
  49. G. DrawString (vstr, ft, textBrush,
  50. NewRectangleF (0, 0, 128,128 ),
  51. NewStringFormat (StringFormatFlags. NoWrap ));
  52.  
  53. Img. Save (context. Response. OutputStream, ImageFormat. Jpeg );
  54.  
  55. Context. Response. Flush ();
  56.  
  57. BackBrush. Dispose ();
  58.  
  59. TextBrush. Dispose ();
  60.  
  61. G. Dispose ();
  62.  
  63. Img. Dispose ();
  64.  
  65. Return;
  66.  
  67. }
  68.  
  69. //************// 
  70.  
  71. Private String_ GetViewString (HttpContext context)
  72.  
  73. {
  74.  
  75. StringStr = context. Request. RawUrl;
  76.  
  77. IntL1 = str. LastIndexOf ("/");
  78.  
  79. IntL2 = str. LastIndexOf (".");
  80.  
  81. ReturnStr. Substring (l1 + 1, l2-l1-1 );
  82.  
  83. }
  84.  
  85. }
  86.  
  87. }

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:

 
 
  1. ﹤httpHndlers﹥  
  2.  
  3.       ﹤add verb="*" path="*.img" type="IBuySpy.IMG,IMGHttphandler" /﹥  
  4.  
  5. ﹤/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.

  1. Introduction to ASP. NET Custom Controls
  2. ASP. NET Server Control View
  3. Detailed explanation of the lifecycle of ASP. NET components
  4. Analysis on Transmission Mechanism of ASP. NET Component Design
  5. Analysis on complex attributes and status management of ASP. NET Component Design

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.