ASP. net2.0 uses httphandler to implement URL rewriting (pseudo URL and pseudo static)

Source: Internet
Author: User
Tags dotnet

We sometimes see this address: Ghost Dotnet-Web-config.aspx In fact, it may not exist, but what you may see is "post. aspx? Id = 34 ", why? There are multiple reasons: First, enhance the friendliness of the URL, remember" Dotnet-Web-config.aspx "Is better than" post. aspx & id = 34? 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 the simplest implementation method I know under ASP. net2.0: The Implementation interface "ihttphandler" to take over HTTP requests
1. Add a Class, Class Code As follows:
// Class urlrewriter Program List:
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 Parameter
String url = context. Request. rawurl;
// Create a regular expression
System. text. regularexpressions. regEx Reg = new system. text. regularexpressions. regEx (@ "/show-(\ D + )\.. + ", system. text. regularexpressions. regexoptions. ignorecase );
// Match with a regular expression
System. Text. regularexpressions. Match m = reg. Match (URL, URL. lastindexof ("/"); // match from the last "/"
If (M. Success) // match successful
{
// Context. response. Write (@"~ /Aspx/show. aspx? Type = "+ M. Groups [1] +" & id = "+ M. Groups [2]);
Context. rewritepath (@"~ /Aspx/show. aspx? Type = "+ M. Groups [1] +" & id = "+ M. Groups [2]); // rewrite path
}
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.cn
Public 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 "/>
</Httphandlers>
[Edit huoho. com]
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 ghost? What about type = 12 & id = 34 ?!
The above settings match the aspx file, because in IIS. the default HTML extension is ASP. net. If you want to take over HTML requests, set the IIS extension. HTML maps to "C: \ WINDOWS \ Microsoft. net \ framework \ v2.0.50727 \ aspnet_isapi.dll ", and then change the above aspx to HTML:
<Httphandlers>
<Add verb = "*" Path = "*/show -? *-? *. Html "type =" urlrewriter "/>
</Httphandlers>
Open the website and enter http://www.supidea.com/post/dotnet-web-config.aspx #

Related Article

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.