It seems that when everyone writes this class, they will also write it to another HttpModule, But I suddenly want to be lazy and just talk about HttpHandler.
Maybe a problem that many dishes haven't thought about. How does IIS process files with different extensions in HTTP?
HttpHandler is actually processing requests for a specific file type. For example, in. net, you can process. aspx page requests so that the output of the entire page processing result is replaced.
In. in. net, when an http request arrives at the server, IIS will determine which application processes the request based on the requested file extension and send it to the client after processing (if there is no corresponding processing program, that is, the file is directly sent to the customer service without any processing. If the other request file does not exist, the Error 404 is returned ), this processing app is the familiar ISAPI (in asp.net, It is aspnet_isapi.dll to execute this operation ).
In IIS, we can map the corresponding file extension to asp.net to process the corresponding file extension request. Of course, this operation is relatively simple, and this is not covered in this article.
Now it's time for code. To create HttpHandler, we all know that it must be a class to implement the IHttpHandler interface.
Take the official example:
public class HelloHttpHandler : IHttpHandler {
public bool IsReusable {
get { return true; }
}
public void ProcessRequest(HttpContext context) {
//do something
}
}
Here, the IsReusable and ProcessReques methods must be rewritten.
The ProcessReques method is the method for processing the request and making a response. This method also imports the context HttpContext of the current request.
You can obtain request-Related Object Information Based on the input context object.
IsReusable, which is explained by the term "cache" by most developers. According to official information, it indicates whether the HttpHandler instance can be reused by multiple requests, or whether a new instance should be created each time.
Then, most of my friends seem to have only implemented IHttpHandler. In fact, there are several other ones that may be quite common. One of them is to implement IHttpHandlerFactory interface processing (of course, you can also inherit the PageHandlerFactory class,
Same principle)
Now let's look at the implementation of this interface. We can look at the relevant processing code. The GetHandler method was called at the beginning of the request,
ReleaseHandler is called when the request ends and all Handler is no longer needed.
If you understand the above two processing methods, asynchronous HTTP processing is also easy to understand.
public class YourHandlerFactory : IHttpHandlerFactory
{
public IHttpHandler
GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
{
throw new NotImplementedException();
}
public void ReleaseHandler(IHttpHandler handler)
{
throw new NotImplementedException();
}
}
Of course, you can also implement the IHttpAsyncHandler interface. As the name suggests, it is obviously an asynchronous IHttpAsyncHandler interface,
I don't think too much here. The official website also has relevant information:
http://msdn.microsoft.com/zh-cn/library/ms227433%28v=VS.90%29.aspx
The web. config file configuration is as follows:
<configuration>
<system.web>
<add verb="*" path="*.aspx"
type="YourHttpHandlerNameSpace.HandlerClassName, YourAssembly" />
</system.web>
</configuration>
Here, I would like to explain:
Verb "*" indicates that all (get, post) requests are processed. Path indicates processing the corresponding file,
"*. Aspx" indicates that requests on the ASPX page are processed. Another point is to specify the path, such as/handler/*. aspx.
Then, we can easily work with the aspnet_isapi.dll method mentioned at the beginning, and then combine with the processing of HttpHandler,
Custom file processing has been added. For example, you can add custom extension files such as path = "*. abc". (Of course, you also need to add a program shadow in the IIS application configuration)
Obviously, HttpHanlder is one type. How can this problem be solved? Can it be said that it is actually a method of half-way breakpoint interception processing?
If you want to replace it, use HttpModule ~~