First, the preface
This simple tutorial really happened to me when I was writing another page command. And then came in and looked at what it was, and found that it was only possible to use. ashx, and only in the application for this file to be able to respond. 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 by simply preparing for it:
1. You can customize the user's access to the Web page response to the file and send the data to the client
2. You can define a handler for all request files that have a consistent suffix name
3. with synchronous and asynchronous selection
I am sure that some people have no understanding of the above, I need to explain the importance of it, then you will naturally know its flexibility.
Let's start with an example:
1. For example, you visit http://www.****.com/web1.msll
You must have found out the problem. Suffix name is. msll you must have seen the Web page by now. This is indeed a feature of the custom HTTP handler, which can be forged by itself. This time you will think 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 a He.cs class, and then I bind it to HEHEHE.PPD so when you visit HTTP://WWW.*****.COM/HEHEHE.PPD, the He.cs output is the response to this page.
2. We still assume that you visit HTTP://WWW.****.COM/LIST1.SSM
Http://www.****.com/list2.ssm
Http://www.****.com/list3.ssm
If I tell you that three seemingly different pages are actually the output of an HTTP handler, this is a good idea if I write a Bbsss.cs class and implement the functionality. And then I bind it to *.SSM you can see here is *.SSM this time you just follow your normal thinking, just as long as the suffix name is. The SSM page applications are all using the same Bbsss.cs class, which is not interesting
II. Registration and binding
Why do we have these two parts? And still want to register and bind these two?
The answer is that you only write a class vs it is impossible to know what you are doing, so we need to register our custom HTTP handler in Web.config. The binding is to let IIS know that our site contains a custom HTTP handler. (I'll take IIS7 as an example to illustrate how to bind)
1. Registration
Copy Code code as follows:
<configuration>
<system.web>
<add verb= "*" Path= "<!--here write the page (*.smm,*.ffs,web1.ffe)-->" type= "<!--write Handler's class name-->"
</system.web>
</configuration>
I have used the comments to write all the above parts
2. Binding (IIS7)
1 Open IIS7-"Open the Site node-" Click on the name of your site
2) Double click
3) Click
4)
5 Finally Click OK so the binding in IIS is complete (the complete example below I will describe the process in text)
Third, on the implementation of the class attention point
Although we have introduced the registration and binding, but not all, about the implementation of the class also have certain requirements. ProcessRequest methods and IsReusable properties must be implemented according to the instructions in the MSDN documentation
About ProcessRequest can be temporarily understood as Page_Load (even if he is lower than Page_Load)
The second is that there is no built-in Request and Response, there may be some people are going to crash how to write Ah, very simple problem without built-in we define ourselves.
The type of the isreusable is of type bool, and as long as implementation of get does not need to implement set, if true means that using the thread pool if false means not to use the
There is also a requirement for the placement of this class file to be placed under the App_Code folder (this special folder can be created directly from the VS site)
Iv. Complete examples (create new classes by yourself)
1.
The following is a concrete implementation of the HelloWorldHandler.cs class (placed under the App_Code folder)
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
<summary>
Summary description 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
{
Get
{
return false;
}
}
}
2.Following is
Web.configof the content
Copy Code code as follows:
<add verb= "*" path= "*.sample" type= "HelloWorldHandler"/>
3.IIS configuration (don't forget to deploy your project to IIS)
The following are specific configurations
Then test http://localhost/SampleApplication/test.sample (port is 80 o'clock)
You can see your results.
V. End of
You have basically mastered the custom synchronization HTTP handler, of course, this is only a small part, we will also contact with asynchronous, because the synchronization in the user access to a large number of cases will make the server resources quickly depleted, but also to reduce the user's usefulness