Understand and customize HttpHandler

Source: Internet
Author: User
Tags metabase

Preface

I have previously found several articles on How to customize HttpHandler on the Internet, but I have never succeeded. After summing up the previous article "one of the asp.net pipeline models (pipeline models) cannot be packed up", I have a better understanding of the pipeline model and the request/response process, and then combined with Asp. net Architecture (Http Handler introduction)-Part.2 and finally successfully defined their own HttpHandler and HttpHandlerFactory ^_^,

Some parts of this article will overlap with asp.net pipeline model (pipeline model), but the angle will be different. The two articles complement each other, we hope to provide a complete picture of the pipeline model.

Directory

1. The HttpHandler process is obtained by Default when Default. aspx is sent;

2. How to configure;

3. Custom HttpHandler;

4. Customize HttpHandlerFactory;

5. application instance.

The HttpHandler process is obtained by Default when Default. aspx is sent.

IIS has a metabase file, which can be added, modified, or deleted through website-> properties-> Home Directory-> Configuration-> ing.

Figure 1

IIS checks whether the request file contains a corresponding processing application in the metabase file. The worker forwards the request to the Asp.net workflow. Jump to the HttpApplication of the pipeline model (there are still many steps before, here focuses on the steps after HttpApplication), HttpApplication calls the corresponding HttpHandlerFactory according to the Url extension name (by default. aspx calls System. web. UI. pageHandlerFactory ,. ashx calls System. web. UI. simpleHandlerFactory), and then HttpHandlerFactory generates default. the specific HttpHandler instance of aspx.

How to configure

I 've been lost in how to configure it before. Let's first understand it!

The following is the configuration basis for custom HttpHandler:

IIS performs the first filtering and HttpApplication performs the second filtering. So

1. Configure iis to apply the corresponding file suffix to asp_isapi.exe for processing;

2. Configure the <HttpHandlers> node in the application-level web. config so that HttpApplication can submit the request to the corresponding HttpHandlerFactory or HttpHandler for Processing Based on the configuration.

Specific web. config Configuration:

1 2   <add verb="*" path="*.myhandler" type="My.MyHandler,MyAssembly"/>
3

Verb: request action (get, post ,*);

Path: file path (* wildcard );

Type: The preceding parameter is the complete class name (including the namespace) of the custom HttpHandler or HttpHandlerFactory ),, the following parameter is the name of the set where HttpHandler or HttpHandlerFactory is located (excluding the dll suffix ).

 

Custom HttpHandler

Define a class that inherits the IHttpHandler interface and implement the interface:

 1 public class MyHandler:IHttpHandler
2 {
3 public MyHandler()
4 {
5 }
6
7 public void ProcessRequest(HttpContext context)
8 {
9 context.Response.ContentType = "text/html";
10 context.Response.Write("MyHandler");
11 }
12
13 public bool IsReusable
14 {
15 get { return true; }
16 }
17 }

1. The ProcessRequest method is used to process the request;

2. IsReusable identifies whether the HttpHandler class instance object can be reused. That is, when the first request is sent, an HttpHandler instance object is instantiated. If IsReusable is true, the second request can be sent back to the instance object without re-constructing one, if this parameter is set to false, a new instance is required. (It's a bit cool (* ^__ ^ ......) Returns true, improving efficiency !!

Configuration in web. config

1 <Handlers>
2 <add verb="*" path="*.gif" type="MyHandler,MyHandler"/>
3 </Handlers>

In iis, upload an ing item in Figure 1 and extend the extension name to .gif. the executable file path is C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ ghost. Otherwise, 404 is returned ).

Note: The Custom HttpHandler must be placed in another project and referenced to a Web project. That is to say, the type Assembly name in the configuration file must be written; otherwise, it cannot be executed.

Custom HttpHandlerFactory

Define a class that inherits the IHttpHandlerFactory interface and implement the interface:

1 public class MyHandlerFactory: IHttpHandlerFactory
2 {
3 public MyHandlerFactory ()
4 {
5}
6
7 # region IHttpHandlerFactory Member
8
9 public IHttpHandler GetHandler (HttpContext context, string requestType, string url, string pathTranslated)
10 {
11 return new MyHandler ();
12}
13
14 public void ReleaseHandler (IHttpHandler handler)
15 {
16 throw new NotImplementedException ();
17}
18
19 # endregion
20}

1. GetHandler gets a new HttpHandler instance;

2. ReleaseHandler allows the factory to reuse existing handler instances.

Configuration in web. config

1 <Handlers>
2 <add verb="*" path="*.gif" type="MyHandlerFactory,MyHandlerFactory"/>
3 </Handlers>

You do not need to configure MyHandler here.

In iis, upload an ing item in Figure 1 and extend the extension name to .gif. the executable file path is C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ ghost. Otherwise, 404 is returned ).

 

Application Instance

Asp. Net architecture (Http Handler introduction)-In Part.2, an instance using HttpHandler to prevent image leeching is very practical!

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.