Use the ihttphandler interface to [rewrite different paths + Any URL suffix to the specified page without changing the URL address] (with source code) _ ax

Source: Internet
Author: User

[sequence]
vs2003
after two days, we finally got it done! Thank you, Mr. Wei !! Thank you, Manager liang for telling me ihttphandler and ihttpmodule these two things have never been used before, In fact, Mr. Wei is generally responsible for writing documents ....
[requirement]
use different URLs to correspond to a page file, as shown in
Source Path: http://www.IHttpHandler.com/target/target.aspx
use the following address to reach the page:
different suffixes: http://www.IHttpHandler.com/target/target.ax
different paths: http://www.IHttpHandler.com/target.ax
http://www.IHttpHandler.com/sadfsadfsa/safdsaf/target.ax

These URLs all point to the/target. ASPX page of the website.
[Start]
① Vs2003 creates an ASP. NET solution named ihttphandler
Create a folder in the ihttphandler project: Target
Create a webpage in the target Folder: target. aspx
② Add a new[Class Library] project: axzhz
Delete automatically generated class files
Create a class file: ax
This is a bit tricky.
I. In order to keep the URL unchanged during page jump, I used the server. Transfer () method, but this method must inherit from the system. Web. UI. Page class.
II. The system. Web. UI. Page class is also implementedProcessrequest () method and isreusable attribute,They must be overwritten with the new keyword.
For specific page turns, you can follow the business logic or use regular expressions. We can use them as needed!

1 Using System;
2 Using System. collections;
3 Using System. componentmodel;
4 Using System. Web;
5 Using System. Web. sessionstate;
6 Public   Class Ax: system. Web. UI. Page, ihttphandler
7 {
8 // If the URL is *. Ax, run the following method:
9 Public   New   Void Processrequest (httpcontext context)
10 {
11 /**/ /*Current context request
12After deletion, the following error occurs:
13Exception details: system. Web. httpexception: the request is unavailable in this context*/
14 Httprequest request = Context. request;
15
16 // Get the content after the Domain Name
17 String Strpathandquery = Request. url. pathandquery;
18 // Retrieve the last page name of the URL
19 String Filename = Strpathandquery. substring (strpathandquery. lastindexof ( " / " ), Strpathandquery. lastindexof ( " . " ) - Strpathandquery. lastindexof ( " / " ));
20 // Set the webpage to be redirected
21 Server. Transfer ( "~ /Target " + Filename + " . Aspx " );
22 /**/ /*In the following sentence, any URL suffixed with. Ax can be switched
23A fixed relative path on the webpage target. aspx*/
24 // Server. Transfer ("/target. aspx ");
25
26 }
27 // Httphandler is set to synchronous. Synchronous processingProgramAfter processing the HTTP request for which the handler is called, it will be returned.
28 Public   New   Bool Isreusable
29 {< br> 30 Get { return false ;}
31 }
32 }
33

③ Return to the ihttphandler project and modify the Web. config file,Add a , As shown in the following figure. Note: Adding nodes is not complete.CodeOnly indicates the node under which the node is added.

The items in the

Attribute Description
Path The path attribute can contain a single URL path or a simple wildcard string (for example, *. aspx ).
Type Specifies a comma-separated class/assembly combination. ASP. NET first searches for the Assembly DLL in the dedicated \ bin directory of the application, and then searches for the Assembly DLL in the system assembly cache.
Predicate The predicate list can be a comma-separated HTTP Method list (for example, "Get, put, post") or start script ing (for example, wildcard * [asterisk]).
1 < Configuration >
2 < System. Web >
3 < Httphandlers >
4 < Add verb = " * " Path = " *. Ax " Type = " Ax, axzhz "   />
5 </ Httphandlers >
6 < System. Web >
7 < Configuration >

④ Add a reference to axzhz in the ihttphandler project, as shown in figure

⑤ The most important step is to register the. Ax suffix on IIS, which wastes a lot of time !!!!
On your website(I won't talk about configuring the website. If not, see:
Http://www.cnblogs.com/AXzhz/archive/2006/10/25/539371.html)
Right-click on the page to open the Properties dialog box → home directory tag → configuration → add]
Executable File Path: C: \ WINDOWS \ Microsoft. NET \ framework \ v1.1.4322 \ aspnet_isapi.dll

Remember to remove the check box "check whether the file exists !!! Debugging is laborious !!....

6 OK, press F5, gogogogogo ...................

[Materials] I don't like copy/paste. I will send you an msdn link. I will see the page associated with this link:
Http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/cpguide/html/cpconhttphandlerregistration. asp

[Summary]
I. msdn is good, but its description is not very detailed,
For example, step 5
It includes one sentence: Make sure that the httphandler file extension is registered in Internet Information Service (IIS.
Ii. deepen the understanding of the page access process:
Enter the URL and press ENTER → IIS matches the configured extension → if the extension exists, call the extension. DLL file → web. Http://www.cnblogs.com/AXzhz/archive/2006/08/15/477668.html In fact, there is no need to remember this clearly. I can't tell myself what I wrote. I just want to know the general meaning.
Iii. Cooperating with colleagues and studying together, the collective power is powerful. The three Zhuge lights are always better than each other.
For example, in this post, Mr. Wei will register the IIS extension, and I will know the server. tansfer () This method can jump without changing the URL (this is what my colleague Li told me)
Iv. Be careful. It is compromised by the default options of IIS.
[Check whether the file exists] It is selected by default.
V how to adapt to the trend in vs2005?
I really want to do it on vs2005, and the porting is not successful: it cannot be done in ASP. net project, you can use the CSC command to generate a DLL file, reference it, but I do not know what the parameter is, I hate the command line, and vs2005 ASP.. Net project. you cannot call a folder created by yourself...No indications for the new version
Vi True experts do not like to write blogs, why?
I want to write these simple things, but these things must be mastered by cainiao. Therefore, I want to write down what I have learned and share it with beginners.
VII. There is something in it that cannot be written. It can only be understood by cainiao.
VIII. This method involves the ihttphandler interface and has a preliminary understanding of it,

[Endless]
This method is not very concise. In the next article, I will use the rewritepath () method to override this function.
Let's continue with our next book, so stay tuned!

[Append] I found a good resource and wrote it quite well. It was a little long. I didn't have any confidence at last.
Http://www.aspxboy.com/484/archive.aspx

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.