See on the net, a lot of friends do urlrewrite in asp.net, use Httphandle+server.transfer method. In fact, this method is wrong. First, the httphandle can not achieve the Urlrewrite, the second server.transfer is the standard redirect, is not urlrewrite at all.
In fact, the implementation of Urlrewrite do not have their own httphandle, also do not have to implement their own httpmodule, with a few lines of code can be easily achieved.
I am here to introduce on the virtual host, the virtual host is different from their own server, you do not have the authority to modify IIS, and do not have permissions to install IIS rewrite and so on IIS Plug-ins. But we can still easily complete the required functionality.
The practice is as follows: Open Global.asax.cs, navigate to protected void Application_BeginRequest (Object sender, EventArgs e). From the method name I guess I can guess what it does. Enter the following code:
protected void Application_BeginRequest (Object sender, EventArgs e)
{
string oldurl = HttpContext.Current.Request.RawUrl;
String pattern = @ "^ (. +) default/(\d+) \. ASPx (\?. *) *___fckpd___0quot;;
string replace = "$1default.aspx?id=$2";
if (Regex.IsMatch (Oldurl, pattern, regexoptions.ignorecase regexoptions.compiled))
{
String newurl = Regex.Replace (Oldurl, pattern, Replace, regexoptions.compiled
Regexoptions.ignorecase);
This. Context.rewritepath (Newurl);
}
With the above code, I access a similar: .../default/123.aspx URL, of course, this site does not exist on my computer, it will be directed to: .../default.aspx?id=123.
Of course, with powerful regular expressions, you can rewrite URLs to your own needs,
All of this is done silently on the server side and there is no awareness of the client at all. Because it is on a virtual host, we can only redirect. aspx file, if it is its own server, as long as the suffix name in IIS registration, you can implement any suffix name processing. For example, you can register a *.myweb type so that when someone accesses Default/456.myweb, you can redirect it to default.aspx?id=456. In a word, as long as you can think of. NET can help you achieve, and all this does not need how much code.