How to Use asp.net Urlrewriter on a VM

Source: Internet
Author: User

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.

Related Article

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.