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.

but we can customize httphandler to expand this support.
to study httpmodule and ihttphandler, you must first understand ASP. NET processing pipelines.
in ASP. in the Program Of the. NET application, the system uses a group of related classes to process client requests (requests) and 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 in the system. the following types are defined in the Web namespace: The httpworkerrequest abstract class defines ASP.
httprutime provides a set of services for processing applications.
httpcontext stores all the context information related to processing a request;
httpapplicationfactory provides applications for related directories.
httpapplication defines all ASP. NET applications. This class is also in the user's global. the base class of the application defined in the asax file;
modules handles 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) createSimplehandlerProject, add a classMyhandler. CS,CodeAs 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 and generate the above CodeSimplehandler. dllFile;

3 ). Create a newWebProject:Simplehandler. dllAdd to project reference;
4 ). ModifyWeb. config, Add the following content:
<System. Web>
<Httphandlers>
<Add verb = "*" Path = "*. aspx" type = "simplehandler. myhandler, simplehandler"/>
</Httphandlers>
</System. Web>
Options in the configuration file: 
VerbYes"Get"Or"Post", IndicatesGetOrPost.
"*" All requests are processed.  
Path Indicates processing the corresponding file, "*. Aspx" Send to all Aspx Page. You can specify the path, as shown in figure "/Test/*. aspx" , Indicates that only Test Directory Aspx File Processing.
Type Attribute, specified by the string before the commaHttphandlerClass Name of the implementation class, which is specified by the stringDLLFile Name.
Request anyAspxPage, the page always displays only one line:My first handler!

Because our custom Handler Intercepted all sending directions Aspx Page requests, and use your own method to process these requests.

To make ourWord Definition Page For example : *. AAA To run smoothly, we need to modify Web. config File: 
 
<System. Web>
<Httphandlers>
<Add verb = "*" Path = "*. AAA" type = "simplehandler. myhandler, simplehandler"/>
</Httphandlers>
</System. Web>
To make the suffix . AAA The file request can be Handler We also need some additional work to intercept the operation. Open IIS Management console, and click site, select " Attribute " . The site Properties dialog box is displayed. Select the Home Directory option, Select Configuration. The application configuration dialog box is displayed. ". AAA" Add to application ing. Now we can add. AAAFile. When a request is sent to the file, the browser displays: My first handler!For otherAspxFile Access 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.