J - C Sharper
J - C Sharper

Reputation: 1697

HTML HREF, how to set relative path from the root (without using ".." to move up)?

For Example, if I have this in my aspx file:

<link href="../Css/StyleSheet.css" rel="stylesheet" type="text/css" />

with ".." indicate to go up one directory level.

Whenever I move the aspx page to different directory level, I have to set it again. How do I set it so that it will be relative to the root of the project folder? (So that either if I move the project folder to somewhere else and/or moving the aspx page to upper/lower directory level, I won't have to set it again?

Upvotes: 3

Views: 13930

Answers (2)

Keefe Higgins
Keefe Higgins

Reputation: 177

You should be able to use Page.ResolveUrl:

    <link href="<%= Page.ResolveUrl("~/Css/StyleSheet.css") %>" rel="stylesheet" type="text/css" />

Or possibly turn it into server control and be able to use the tilde:

    <link href="~/Css/StyleSheet.css" runat="server">

Upvotes: 10

Oriol
Oriol

Reputation: 288680

You can start your URLs with /:

<link href="/Css/StyleSheet.css" rel="stylesheet" type="text/css" />

Upvotes: 0

Related Questions