asp.net urlrewriter on the virtual host use method _ Practical skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.