Post) httphandler httpmodule makes a webpage with its own suffix!

Source: Internet
Author: User
The web we use ASP. NET usually has a. aspx suffix. But do you want to have another Suffix of your own? Let's study it together!

First, let's talk about some parameter passing and page targeting methods in Asp.net.
First, Asp.net uses page. navigate () to call the URL of the new page. Page. navigate () returns an HTTP status code 302 to the browser, allowing the browser to request the new URL to the server. This navigation method requires two rounds between the client and server for each customer request.
Second, any information to be passed to the new page must be used as URL parameters, stored in sessions, or stored in the database for the new page to obtain the information. Traditional ASP developers are used to this practice, but other web programmers have more advanced methods. However, the two pages are obviously dependent, and the dependency is not captured by the compiler and is not easily modeled at the design stage. Therefore, when debugging, whether the parameter is correctly passed is checked by ourselves. In addition, traditional data transmission methods may expose some key data. More importantly, this makes the object-oriented design very complex.

However, we can customize httphandler to expand this support.
To study httpmodule and ihttphandler, you must first have an understanding of ASP. NET processing pipelines.
In ASP. in the. NET application, the system uses a set of related classes to process client requests (requests), Asp. NET application processing mode can be called HTTP processing pipeline.
Httpmodule and ihttphandler are two processes in the processing pipeline. Classes in the HTTP processing pipeline are defined in the system. Web namespace and mainly include the following types: The httpworkerrequest abstract class defines the basic methods for processing requests on ASP. NET pages;
Httprutime provides a set of services for processing applications;
Httpcontext stores all context information related to a request;
Httpapplicationfactory provides applications in related directories;
Httpapplication defines general methods, attributes, and events for all ASP. NET applications. This class is also the base class of the application defined in the user's global. asax file;
Modules processes pre-request and post-response events;
Handlerfactories provides handlers in the application;
Handlers processes requests and responses.

On Windows, HTTP pipline requires IIS support. To run ASP.. NET application. IIS requires the following two files: aspnet_isapi.dll and aspnet_wp.exe aspnet_isapi.dll are an ISAPI extention that will be sent to IIS. Please transfer aspnet_wp.exe to process aspnet_wp.exe and use httpruntime to process the request as follows:

The following is a simple example!

1) create a project named simplehandler and add a class myhandler. cs. The Code is as follows:
Namespace simplehandler
{
Public class myhandler: ihttphandler
{
Public void processrequest (httpcontext CTX)
{
Httpresponse response = CTX. response;

Response. Write ("My first handler! ");
}
Public bool isreusable
{
Get {return true ;}
}
}
}

2) Compile the above Code to generate the simplehandler. dll file;

3) create a new web project and add the simplehandler. dll file to the reference of the project;
4) modify web. config and add the following content:
<System. Web>
<Httphandlers>
<Add verb = "*" Path = "*. aspx" type = "simplehandler. myhandler, simplehandler"/>
</Httphandlers>
</System. Web>
Options in the configuration file:
Verb can be "get" or "Post", indicating that the get or POST request is processed.
"*" Indicates that all requests are processed.
Path indicates processing the corresponding file. "*. aspx" indicates processing the requests sent to all aspx pages. You can specify a path, such as "/test/*. aspx", indicating that the aspx file under the test directory is processed only.
In the type attribute, the string before the comma specifies the Class Name of the httphandler implementation class, and the subsequent string specifies the name of the DLL file.
Now, on any ASPX page in the request project, there is always only one line: My first handler!

Because our Custom Handler intercepts all requests sent to the ASPX page and uses its own method to process these requests.

To make our word definition page run smoothly, for example, *. AAA, we need to modify the Web. config file:
 
<System. Web>
<Httphandlers>
<Add verb = "*" Path = "*. AAA" type = "simplehandler. myhandler, simplehandler"/>
</Httphandlers>
</System. Web>
In order to make requests for files suffixed with. AAA be intercepted and run by our handler, we still need some additional work. Open the IIS console, click the site, and select "properties". The site Properties dialog box is displayed. Select the main directory option and select configuration. The application configuration dialog box is displayed ". AAA "added to application ing. now we can add. AAA file. When sending a request to this file, the browser displays: My first handler! Access to other aspx files is not affected.

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.