Summary of Url Relative Path Problems

Source: Internet
Author: User

The most worrying thing is that you sometimes have to use an absolute path (such as the master content in asp.net). Because there is no target address in the development process, you cannot configure an absolute path. Sometimes relative paths must be used.

However, the relative path is well written, but there are various problems.
Today, I will introduce two different methods.
First, we will introduce the newly added Base tag in HTML5.
The usage must be included in the head label.

Copy codeThe Code is as follows: <Base target = "_ blank" href = "www.Test.com"/>
</Head>

Most importantly, after the base tag is configured, the browser no longer uses the relative URL in the current document, but uses the specified basic URL to parse all relative URLs.
If the URL in your <a>, , <link>, and <form> tags is "chamychen.jpg ", the browser will automatically add the specified href address in the base tag before the URL when linking the URL. The method of opening the address will also follow the target attribute set in the Base tag. After that, we can set the base tag in master,mvcin layout.shtml to solve the absolute path and Relative Path Problems. In the future, we can set all the relative paths to start from the root directory of the website. You can configure the URL in Base to solve all the problems of relative paths and absolute paths.
For browsers that do not support html5, we can use C # To write a few lines of code to get the address of the website root directory:Copy codeThe Code is as follows: public static string HostUrl ()
{
String path = HttpContext. Current. Request. Url. Scheme + ": //" +
HttpContext. Current. Request. Url. Authority +
(HttpContext. Current. Request. ApplicationPath = "/"? "": HttpContext. Current. Request. ApplicationPath );
Return path;
}

To sum up, you only need to use the "url + relative path" method to handle problems caused by relative paths.
Such as: http://www.Test.com/ AB /abc.jpg
You can use the Base Tag:
<Head>
<Base target = "_ blank" href = "www.Test.com"/>
</Head>

Relative Path:
If multi-C # is used for support: HostUrl () + "AB/abc.jpg"

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.