Asp.net| Virtual Host
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 (\?.) *)*$";
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, the use of powerful regular expressions, you can arbitrarily according to their own needs to rewrite the URL, all of which are silently on the server side, the client will not have any awareness. 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.