Use the rewritepath method to [rewrite different paths + Any URL suffix to the specified page without changing the URL address] (with source code) _ ax

Source: Internet
Author: User

The requirement of the previous article is much simpler.
[STEP]
① Create a test directory in the project, and then create a page under the test directory: ax. aspx
② Add the application_beginrequest () method to global. asax. CS.

Protected   Void Application_beginrequest (Object sender, eventargs E)
{
String Originalurl, newurl;
Originalurl = Request. url. pathandquery;

// Get the Page name, excluding the extension
Newurl = Originalurl. substring (originalurl. lastindexof ( ' / ' ), Originalurl. lastindexof ( ' . ' ) - Originalurl. lastindexof ( ' / ' ) + 1 );
Newurl = " ~ /Test " + Newurl + " Aspx " ;
/**/ /*This sentence is the essence
【~ /] Indicates the root directory of the website.
It's really strange. It used to indicate that the root directory does not use 【~] You can,
However, if the following statement is executed, an error is returned:
The virtual path "/test/ax. aspx" is mapped to another application, which is not allowed.*/

Httpcontext. Current. rewritepath (newurl );
}

③Register the. Ax suffix on IISFor more information, see chapter 5.
④ Configure the website for this project (see my previous post for details). In this example, the website name on my host is www.rewritepath.com.
⑤ Test, enter the following URL
Http://www.rewritepath.com/AX.ax
Http://www.rewritepath.com/AX.aspx
Http://www.rewritepath.com/sadfsa/sdf/AX.ax
Http://www.rewritepath.com/sad/fsa/sdf/AX.aspx
Actual path
: Http://www.rewritepath.com/Test/AX.aspx
Test passed, OK, very good!

[Expansion]
I. You can use regular expressions to rewrite URLs with different rules.
II. This method can be used as an overload method to implement extension with Parameters

C #
Public

      Rewritepath(

      Filepath,

      Pathinfo,

      Querystring)

Parameters
FilepathInternal rewrite path.

PathinfoThe additional path information of the resource.

QuerystringQuery string.

I originally wanted to use this method to redirect between pages, and the result failed., Error:
Data cannot be verified.
But I can see the page Jump section on msdn.CodeI don't know how it works.

[Summary]
This is a simple and practical fast food. the method itself enables page Jump and the URL in the address bar remains unchanged. The previous article uses ihttphandler, which is somewhat close to the underlying layer and gives us a deep understanding of ASP. net is advantageous, but it can also implement more powerful functions.

[Appendix] This article references the URL rewriting Technology in ASP. NET 2.0 of Henry Liu, which is actually a simple version of Henry Liu.
URL: http://www.cnblogs.com/lgp/archive/2006/10/16/530426.html

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.