Easily implement urlrewrite on a VM using Asp.net

Source: Internet
Author: User

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.

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.