First, HttpHandle cannot implement urlrewrite;
The second Server. Transfer is a standard redirection, and it is not urlrewrite at all.
In fact, you do not need to implement HttpHandle or HttpModule on your own to implement urlrewrite. You can easily implement it with several lines of code.
I will introduce that on a VM, the VM is different from its own server. You do not have the permission to modify IIS or install iis plug-ins such as IIS rewrite. However, we can still easily complete the required functions.
The procedure is as follows: open global. asax. cs and locate protected void Application_BeginRequest (Object sender, EventArgs e ). From the method name, I can guess what it is. Enter the following code:
Copy codeThe Code is 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 code similar :... /default/123. aspx URL, of course, this URL does not exist on my computer, it will be directed :... /default. aspx? Id = 123.
Of course, with a powerful regular expression, you can rewrite the url as needed,
All of this is done silently on the server side, and there is no perception on the client side. Because it is on the virtual host, we can only redirect the. aspx file. If it is your own server, you only need to register the Suffix in IIS to implement any suffix processing. For example, you can register a type such as *. myweb, 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, it can help you implement it, and it does not require much code.