ASP. NET 3.5 core programming learning notes (45): Advanced Programming for HTTP processing programs

Source: Internet
Author: User

The user of the HTTP handler is clear: changes the processing method of a type of resource and returns it to the user. We can filter traditional resources by processing programs based on runtime conditions or some form of logic, and enable HTTP processing programs to return specific pages or resources asynchronously.

For HTTP handlers, registration is a key step, which provides information about the handler for ASP. NET. However, we all need to modify the Web. config file of the application to provide information about the processing program for the ASP. NET application.

Through the ashx extension and processing program programming model, we can avoid modifying web. config. To deploy a new HTTP handler, you only need to copy the corresponding file to a folder of the application.

Process Programs in the form of ashx Resources

The ashx file contains a special command @ webhandler, which indicates the relationship between the HTTP processing program endpoint and the class implementing it. All ashx files must start with the following command:

<%@ WebHandler Language="C#" Class="Core35.Components.YourHandler" %>

If the ashx endpoint is called, ASP. NET will parse the source code of the file and find the HTTP handler class indicated by the @ webhandler command. This automatic process eliminates the need to update the web. config file.

The following is an example of an ashx file:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
}

public bool IsReusable {
get {
return false;
}
}

}

Note that the source code of this class can be written in the form of inline or loaded from the program referenced by the application.

ASP. NET adds a record to the IIS MetaStore for the ashx resource, so you do not have to configure the web server.

Resources with the ashx extension are processed by the HTTP handler factory named simplehandlefactory. It searches for the @ webhandler command at the top of the file and finds the HTTP handler class through this command.

To build an HTTP handler, We can compile regular class files into an assembly or use ashx resources. Except for the dynamic compilation of ashx resources during the first request, there is no significant difference between the two methods.

Prevent Access to banned Resources

If you do not want to expose resources managed by a Web application through the Web, you must not return those resource files through IIS. Therefore, you can send requests for this resource to the aspnet_isapi and map the corresponding extension to a built-in handler, that is, the httpforbiddenhandler class:

<add verb="*" path="*.xyz" type="System.Web.HttpForbiddenHandler" />

In this way, any access to the. XYZ resource will display an error message. This function also applies to a specific resource in an application, as shown below:

<add verb="*" path="yourFile.txt" type="System.Web.HttpForbiddenHandler" />

How to determine whether HTTP processing programs need to be reused

In general HTTP processing programs, processrequest is responsible for completing all the work. The isreusabel attribute of the ihttphandler interface should be set to true only in special cases. If this attribute is true, the handler will not be detached from the memory and used repeatedly after the handler is used. That is, this attribute is used to indicate whether the handler object can be put into the pool.

In most cases, the return of true or false for this attribute has no significant impact. If this attribute is false, you must create a new object for each request. Creating a simple object does not cause large system overhead. If the code initialized by the handler is high, the isrusable attribute should be returned to true to reduce the initialization overhead.

In general, in addition to processing multiple instance attributes in a concurrent environment, or some attributes may have problems, the isresuable attribute should return true.

When will the isresuable attribute of the processing program be used? The answer is that when an HTTP handler factory is used, the factory will ask the handler to see if it can serve multiple requests and then selectively create and maintain the handler pool.

ASP. NET pages and ashx resources are created under the control of their respective factories. However, neither of these factories checks the isreusable attribute. In the processing factory built in ASP. NET platform, the isreusable attribute of the program is seldom checked. This means that when creating an HTTP handler for an axd, ashx, or aspx resource, you do not need to set a waste of time to try to find the optimal isresuable attribute.

When do I need to use an HTTP processing factory? The answer is: you cannot determine which HTTP handler class to choose. For example, for An ASPX page, we may not be able to further obtain the type of HTTP handler to be used. This type may not exist (if it is compiled dynamically ). Here, the HTTP handler factory needs some logic to decide which handler to use. That is to say, if it is not enough to declare the binding between the endpoint and the class, the HTTP handler factory is required to determine which HTTP handler to use.

HTTP handler Factory

HTTP requests can be directly associated with the HTTP processing program or the factory object of the HTTP processing program. The HTTP handler Factory is a class that implements the ihttphandlerfactory interface and is responsible for returning the HTTP handler that actually processes the request.

In. NET Framework, the HTTP handler factory is used to perform some preparation work on the requested resource before it is passed to the handler. For example, pagehandlerfactory is responsible for processing the ASPX page. This handler factory not only simply returns the corresponding handler, but also loads the handler from the handler set if needed.

The HTTP handler factory implements two ihttphandlerfactory Methods: gethandler and releasehandler. See the following table:

The following is a simple HTTP processing program factory example. The factory can return the request processing program based on different HTTP actions (get or post:

class MyHandlerFactory : IHttpHandlerFactory
{
public IHttpHandler GetHandler(HttpContext context, string requestType, String url, String pathTranslated)
{
if(context.Request.RequestType.ToLower() == "get")
return (IHttpHandler)new MyGetHandler();
else if(context.Request.RequestType.ToLower() == "post")
return (IHttpHandler)new MyPostHandler();
return null;
}

public void ReleaseHandler(IHttpHandler handler)
{
// Nothing to do
}
}

To use the HTTP processing factory, we need to register through the ASP. NET configuration file. If you register a processing program, it will always be used to process the corresponding request. If we select a factory, we can dynamically decide which processing program is more suitable for specific requests based on runtime conditions. In addition, you can use the isreusable attribute of the handler to implement the handler pool.

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.