We can see on the Internet that many friends use httphandle + server. Transfer to perform urlrewrite in Asp.net. In fact, this method is incorrect. First, httphandle cannot implement urlrewrite; second, server. transfer is a standard redirection, not urlrewrite.
In fact, you do not need to implement httphandle or httpmodule on your own to implement urlrewrite. Use several linesCodeIt can be easily implemented.
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:
Protected Void Application_beginrequest (Object sender, eventargs E)
{
String Oldurl = Httpcontext. Current. Request. rawurl;
StringPattern= @"^ (. +) Default/(\ D +) \. aspx (\?. *) * $";
StringReplace= "$ 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 won't be noticed 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 it,. Net can help you implement it, and it does not require much code.