Home > Net >  Difference between ~/ and ../
Difference between ~/ and ../

Time:11-13

No particular error but I'm working on an assignment and when I use ../../Style/index.css the styling does not render to the master page it is linked to. But when I change it to ~/Style/index.css it renders the styling. Is there a difference between the two. I'm new to asp.net

 <link href="~/Content/Master.css" rel="stylesheet" type="text/css" />

as apposed to

 <link href="../../Content/Master.css" rel="stylesheet" type="text/css" />

CodePudding user response:

this link always show the absoulute patch from the root of website, doesn't matter in what folder it is situated

<link href="~/Content/Master.css" rel="stylesheet" type="text/css" />

but this link shows relative path, and depends on the folder where page with this link is situated

 <link href="../../Content/Master.css" rel="stylesheet" type="text/css" />

the page with this link supposed to be in two nested folders from the root. if page moves to somewhere else, the link will be invalid

CodePudding user response:

The main difference between absolute and relative paths is that absolute URLs always include the domain name of the site with http://www. Relative links show the path to the file or refer to the file itself.

A relative URL is useful within a site to transfer a user from point to point within the same domain.

<link href="../../Content/Master.css" rel="stylesheet" type="text/css" />

Absolute links are good when you want to send the user to a page that is outside of your server.

<link href="~/Content/Master.css" rel="stylesheet" type="text/css" />

Developers often make life easier for themselves by using relative links for their site. When there are hundreds of pages on a resource, it is tedious and time-consuming to write the entire path for each one. Instead, indicating a point on the site map will make it clear that the page belongs to a specific server, so in general it's consider best practice to use the absolute path.

  • Related