First, the httphandle is not to achieve urlrewrite;
The second Server.Transfer is the standard redirect, 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:
Copy Code code as follows:
protected void Application_BeginRequest (Object sender, EventArgs e)
{
string oldurl = HttpContext.Current.Request.RawUrl;
String pattern = @ "^ (. +) default/(\d+) \.aspx (\?.) *)*$";
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 Web site, 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 the virtual host, we can only redirect. aspx files, if it is their 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 much code.