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"