Easy implementation of Urlrewrite with ASP.net on a virtual host

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



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.