We sometimes see an address like this: "Why? Type = 12 & id = 34 ", why? There are multiple reasons: First, enhanced urlfriendliness. Remember show-12-34.html "total ratio"/aspx/show. aspx? Type = 12 & id = 34 "is it easy to remember? The second is to facilitate search engine indexing. Many search engines prefer static html pages, such as Baidu. The second is for security reasons because the parameters "type" and "ID" are hidden ". Is it interesting?
In fact, this is implemented using URL rewriting. Next I will talk about it in ASP. what I know in net2.0 is the simplest implementation method: The Implementation interface "ihttphandler" is used to take over HTTP requests, follow me!
1. Add a Class, ClassCodeAs follows:
Program
// Class urlrewriterList:Using system;Using system. Data;Using system. configuration;Using system. Web;Using system. Web. Security;Using system. Web. UI;Using system. Web. UI. webcontrols;Using system. Web. UI. webcontrols. webparts;Using system. Web. UI. htmlcontrols;/// <Summary>/// Urlrewriter URL rewriting class/// Author: Yoyo// Blog: http://yangmingsheng.cn/// </Summary>Public class urlrewriter: ihttphandler // implements the "ihttphandler" interface.{Public urlrewriter (){//// Todo: add the constructor logic here//}Public void processrequest (httpcontext context){Try{// Obtain the original URL blocking ParameterString url = context. Request. rawurl;// Create a regular expressionSystem. Text. regularexpressions. RegEx Reg = new system. Text. regularexpressions. RegEx(@ "/Show-(\ D +) \... +", system. Text. regularexpressions. regexoptions. ignorecase );// Match with a regular expressionSystem. Text. regularexpressions. Match m =Reg. Match (URL, URL. lastindexof ("/"); // match from the last "/"If (M. Success) // match successful{String realpath =@"~ /Aspx/show. aspx? Type = "+ M. Groups [1] +" & id = "+ M. Groups [2];// Context. response. Write (realpath );// Context. rewritepath (realpath); // (rewritepath is used in cookieless session state .)Context. server. Execute (realpath );}Else{Context. response. Redirect (context. Request. url. tostring ());}}Catch{Context. response. Redirect (context. Request. url. tostring ());}}/// <Summary>/// Members required to implement the "ihttphandler" Interface/// </Summary>/// <Value> </value>/// Author: Yoyo// Blog: http://yangmingsheng.cnPublic bool isreusable{Get {return false ;}}}
|
2. Add the setting items in the web. config file.
Add the following code under the <system. Web> node:
<Httphandlers> <add verb = "*" Path = "*/show -? *-? *. Aspx "type =" urlrewriter "/>
|
Explanations:
Verb indicates one or more of the permitted actions "get", "Post", and "put". The asterisk "*" indicates that all actions are allowed;
A path is a matching path that supports Simple wildcards;
Type indicates the bound class name and namespace (if any );
By the way, you must first create a web form "show. aspx" under the directory "aspx", because this file is a page that actually accepts the request and displays the relevant content.
OK !, Compile, open the website input address http: // localhost: 80/show-12-34.aspx access, check to see if it is displayed "/aspx/show. aspx? What about type = 12 & id = 34 ?!
The above settings match the aspx file, because the. html extension in IIS is not taken over by ASP. NET by default. If you want to take over the HTML request,
Map the IIS extension. html to "C: \ WINDOWS \ Microsoft. NET \ framework \ v2.0.50727 \ aspnet_isapi.dll ",
Then, change the above aspx to HTML:
<Httphandlers> <add verb = "*" Path = "*/show -? *-? *. Html "type =" urlrewriter "/>
|
Open the website now and enter the address http: // localhost: 80/show-12-34.html to visit ~!