Asp. NET in the use of segments to get the URL of the filename of a method to share _ practical skills

Source: Internet
Author: User
For example: Http://www.abc.com/book/list.aspx, the corresponding fetch to list.aspx. Generally we can write this:
Copy Code code as follows:

String url = Request.Url.ToString ();
string r = URL. Substring (URL. LastIndexOf ('/') + 1);
Response.Write (R);

The above method is generally acceptable, but there may be a problem if the URL takes a parameter. With Id=3&name=tim, for example, the last thing you get is list.aspx?id=3&name=tim, which is obviously more than just a filename.
As a matter of fact. The URI class provides a segments property that is essentially a string array object that holds the values of the URL path segment, and the last element is the filename.
Copy Code code as follows:
Response.Write (Request.url.segments[request.url.segments.length-1]);

You can experiment, even if the file name is followed by a parameter? Id=3&name=tim, the final gain is still list.aspx.
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.