ASP. NET custom synchronization HTTP Processing Program (Graphic tutorial)

Source: Internet
Author: User

I. Preface

This simple tutorial really happened, as I saw when I was writing another page command. I followed in and looked at what it was, and found that we only use. ashx before, and can only respond when applying for this file. These seem to make it inflexible and do not allow us to use it flexibly. The custom HTTP handler can completely eliminate the above problem. It is just a. cs class file, and you can get a lot of flexibility as long as you follow the simple preparation work:

1.You can customize the response to the file when a user accesses a webpage and send the data to the client.

2.You can define a handler for all request files with the same suffix.

3.Synchronous and asynchronous Selection

I believe that some people may not understand the above. Here I need to explain its significance, so you will naturally know its flexibility.

Let's give an example:

1.For example, you can access http: // www. *****. com/web1.msll.

You must have discovered the problem. The suffix is. msll. You have never seen it since you browsed the web page. This is indeed a feature of the custom HTTP processing program, and the suffix can be forged by itself. At this time, you will think that web1 is the name of the. cs file, but the result is that web1 is not the file name of the. cs file. They are implemented by binding. For example, I implemented an he. cs class, And then I bind it to hehehe. if you access http: // www. *****. com/hehehe. then, the page is responded to by the ppd. cs output result

2.Let's assume that you access http: // www. *****. com/list1.ssm

Http: // www. *****. com/list2.ssm

Http: // www. *****. com/list3.ssm

If I tell you that the three seemingly different pages are actually the output results of an HTTP processing program, this is actually very understandable. If I write a bbsss. cs class and implements related functions. Then I bind it *. ssm, you can find that *. at this time, you should follow your normal thinking, that is, as long as the suffix is. the application on the ssm page uses the same bbsss. cs class. Is it interesting?

Ii. Registration and binding

Why are there two parts? What about registration and binding?

The answer is:It is impossible for you to write only one class vs to know what you are doing, so we need to register our custom HTTP handler in web. config. Binding means that iis knows that our site contains a custom HTTP handler. (I will take iis7 as an example to describe how to bind it)

1. Registration
Copy codeThe Code is as follows:
<System. web>
<Add verb = "*" path = "<! -- Here is the page for applying for binding the client (*. smm, *. ffs, web1.ffe) --> "type =" <! -- Write the handler class name here -->"
</System. web>

I have used comments to write the above self-compiled parts.

2. BIND (iis7)

1) Open iis7-open website node-click the name of your website

2) double-click

3) Click


5) Click OK to complete the binding in iis.(I will introduce the process in text in the complete example below)

Iii. Notes on class implementation

Although we have introduced registration and binding, but not all of them, there are also some requirements on the implementation of classes. The ProcessRequest method and IsReusable attributes must be implemented according to the description in the MSDN document.

ProcessRequest can be understood as Page_Load temporarily (even if it is lower than Page_Load)

The second is that there is no built-in Request and Response. Some people may have to crash. How can we write it? The very simple problem is that we can define it by ourselves without built-in requirements.

IsReusable is of the bool type and does not need to be set as long as get is implemented. If true is returned, the thread pool is used. If false, the thread pool is not used.

There are also requirements for the placement of such files, which should be placed in the App_Code folder (you can directly create this special folder in the vs site)

Iv. Complete example (create a class by yourself)


The following areHelloWorldHandler. csClass implementation (put in the App_Code folder)
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;

/// <Summary>
/// Summary of HelloWorldHandler
/// </Summary>
Public class HelloWorldHandler: IHttpHandler
Public HelloWorldHandler ()

Public void ProcessRequest (HttpContext context)
HttpRequest Request = context. Request;
HttpResponse Response = context. Response;
Response. Write ("Response. Write ("<body> ");
Response. Write ("Response. Write ("</body> ");
Response. Write ("}

Public bool IsReusable
Return false;

2.The following areWeb. configContent
Copy codeThe Code is as follows:
<Add verb = "*" path = "*. sample" type = "HelloWorldHandler"/>

3.IIS configuration (do not forget to deploy the project to iis)

Specific configurations are as follows:

Then test http: // localhost/SampleApplication/test. sample (when the port is 80)

You can see your results.

5. Conclusion

At this point, you have basically mastered the custom synchronous HTTP processing program. Of course, this is only a small part, and we will be exposed to Asynchronization later, because synchronization will quickly exhaust server resources when the user traffic is high, and it will also reduce the user's practicality.

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