ASP. net http module and processing program (3)

Source: Internet
Author: User
How to Use http processing programs when ASP. NET is running

Whether you believe it or not, ASP. NET uses HTTP requests to implement a large number of its own functions. ASP. NET uses a handler to process. aspx,. asmx,. soap, and other ASP. NET files.

The following is a snippet in the machine. config file:

<Httphandlers>
<Add verb = "*" Path = "trace. axd" type = "system. Web. Handlers. tracehandler"/>
<Add verb = "*" Path = "*. aspx" type = "system. Web. UI. pagehandlerfactory"/>
<Add verb = "*" Path = "*. ashx" type = "system. Web. UI. simplehandlerfactory"/>
<Add verb = "*" Path = "*. config" type = "system. Web. httpforbiddenhandler"/>
<Add verb = "Get, head" Path = "*" type = "system. Web. staticfilehandler"/>
......
......
</Httphandlers>

In the configuration information above, you can see that all requests to the. aspx file are processed by the system. Web. UI. pagehandlerfactory class. Similarly, all requests to the. config file and other files (which cannot be directly accessed by the client) are processed by the system. Web. httpforbiddenhandler class. As you may have guessed, when accessing these files, this class simply returns an error message to the client.

Execute the HTTP processing program

Now you will see how to implement an HTTP handler. So what are the tasks for our new processing program? As I mentioned earlier, most handlers are used to add new features to Web servers; therefore, I will create a handler to handle new file types-files with the extension of. 15seconds. After we create this handler and register it in the web. config file of our web application, all requests to the. 15seconds file will be processed by this new handler.

You may be considering how to use this handler. What if you want to introduce a new server scripting language or dynamic server files (such as ASP and aspx? You can compile a processing program for it. Similarly, what if you want to run Java applets, JSP, and some other server-side Java components on IIS? One way is to install some ISAPI extensions (such as Allaire or Macromedia JRun ). You can also write your own HTTP processing program. Although this is a complicated transaction for third-party vendors (such as Allaire and Macromedia), it is an attractive choice because their HTTP processing can access ASP. all new features exposed during the net runtime.

To implement our HTTP handler, follow these steps:

1. Compile a class that implements the ihttphandler interface.

2. register the handler in the web. config or machine. config file.

3. In the Internet Service Manager, map the file extension (. 15 seconds) to ASP. net isapi extension DLL (aspnet_isapi.dll.

Step 1

Create a new C # class library project in Visual Studio. NET and name it "myhandler ". Visual Studio. NET automatically adds a class named "class1.cs" to the project. Rename it "newhandler". Open this class in the code window and change the class name and constructor name to "newhandler ".

The code for the newhandler class is as follows:

Using system;
Using system. Web;

Namespace myhandler
{
Public class newhandler: ihttphandler
{
Public newhandler ()
{
// Todo: add the construction logic here
}

# Region Implementation of ihttphandler
Public void processrequest (system. Web. httpcontext context)
{
Httpresponse objresponse = context. response;
Objresponse. Write ("<HTML> <body> Objresponse. Write ("</body> }

Public bool isreusable
{
Get
{
Return true;
}
}
# Endregion
}
}

You can see in the processrequest method that the HTTP handler accesses all ASP. net internal objects passed to it as parameters through the system. Web. httpcontext object. To implement the processrequest method, you only need to extract the httpresponse object from the context object and send some HTML to the client. Similarly, isreusable returns true, indicating that this handler can be used repeatedly to process other HTTP requests.

Compile the above Code and place it in the bin directory of the webapp virtual directory.

Step 2

In the Web. config file, add the following text to register the handler:

<Httphandlers>
<Add verb = "*" Path = "*. 15 seconds" type = "myhandler. newhandler, myhandler"/>
</Httphandlers>

Step 3

Since we have already created a handler for processing new extended files, we also need to inform IIS of this extension and map it to ASP. NET. If you try to access the hello.15seconds file without executing this step, IIS will simply return the file instead of passing it to ASP. NET runtime. The result is that the HTTP handler will not be called.

Run Internet Service Manager, right-click the default web site, select Properties, move to the Home Directory option page, and click Configure. The application configuration dialog box is displayed. Click the Add button, enter the aspnet_isapi.dll file path in the executable field, and enter. 15 seconds in the extended field. Other fields do not need to be processed. The dialog box is as follows:



Click OK to close the application configuration and default web site Properties dialog box.

Run Internet Explorer and enter URL: http: // localhost/webapp/hello.15seconds. The following page is displayed:

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.