Using ~/ path for to link a CSS page?

Discussion in 'ASP .Net' started by Lance Wynn, Jan 17, 2008.

  1. Lance Wynn

    Lance Wynn Guest

    Hello all,
    Is there a way to use the ~/ path to link a CSS page?

    I thought maybe something like this would work, but no go:
    <link href="~/style.css" rel="stylesheet" type="text/css"/>

    Is this possible to do with the link element?

    Thanks
     
    Lance Wynn, Jan 17, 2008
    #1
    1. Advertising

  2. ~ is filled in by a routine in WebControl (GetClientPath() I think. If not,
    it is close). The link is not attached to a control. YOu can code your own
    implementation of link that use the routine to covnert ~. Otherwise, you
    cannot use ~ to represent route.

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    *************************************************
    | Think outside the box!
    |
    *************************************************
    "Lance Wynn" <> wrote in message
    news:...
    > Hello all,
    > Is there a way to use the ~/ path to link a CSS page?
    >
    > I thought maybe something like this would work, but no go:
    > <link href="~/style.css" rel="stylesheet" type="text/css"/>
    >
    > Is this possible to do with the link element?
    >
    > Thanks
    >
     
    Cowboy \(Gregory A. Beamer\), Jan 17, 2008
    #2
    1. Advertising

  3. Hello,

    From your post, my understanding on this issue is: you wonder how to
    resolve the root directory when we link to a style sheet. If I'm off base,
    please feel free to let me know.

    According to the MSDN article: ASP.NET Web Site Paths
    http://msdn2.microsoft.com/en-us/library/ms178116.aspx
    "ASP.NET includes the Web application root operator (~), which you can use
    when specifying a path in *server controls*. ASP.NET resolves the ~
    operator to the root of the current application. You can use the ~ operator
    in conjunction with folders to specify a path that is based on the current
    root."
    Therefore, as Cowboy replied, the link is not attached to a server control,
    and the operator '~' cannot be resolved as expected.

    Here are two workarounds for your reference:

    Workaround 1: Declare 'runat=server' for the <head> element, and use '~'
    inside it. For instance,
    <head runat="server">
    <title>Untitled Page</title>
    <link href="~/Css/style.css" rel="stylesheet" type="text/css" />
    </head>

    Workaround 2:
    We can link to the style sheet with some server side codes
    (quoted from
    http://www.pluralsight.com/blogs/ted/archive/2005/08/31/14437.aspx)
    public static void AddLinkedStyleSheet(Page page, string stylesheet)
    {
    HtmlLink link = new HtmlLink();
    link.Href = page.ResolveUrl(stylesheet);
    link1.Attributes["text"] = "text/css";
    link1.Attributes["rel"] = "stylesheet";

    page.Header.Controls.Add(link);
    }
    MyClass.AddLinkedStyleSheet(this, "~/css/my.css");

    Please let me know if you have any other concerns, or need anything else.

    Regards,
    Jialiang Ge (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    For MSDN subscribers whose posts are left unanswered, please check this
    document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications. If you are using Outlook Express/Windows Mail, please make sure
    you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
    see your reply promptly.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Jialiang Ge [MSFT], Jan 17, 2008
    #3
  4. Lance Wynn

    marss Guest

    On 17 Січ, 05:08, "Lance Wynn" <> wrote:
    > Hello all,
    > Is there a way to use the ~/ path to link a CSS page?
    >
    > I thought maybe something like this would work, but no go:
    > <link href="~/style.css" rel="stylesheet" type="text/css"/>
    >


    If you use href="/style.css" it refers to a stylesheet resided in the
    root folder of an application
    disregarding of position of the parent page in a website hierarchy.
    IMHO, no need to convert the stylesheet link to a server control, a
    leading slash "/" for HTML elements do the same as "~/" for web/HTML
    controls.

    Regards,
    Mykola
    http://marss.co.ua - Casual ideas for web development
     
    marss, Jan 17, 2008
    #4
  5. Lance Wynn

    John Guest

    Just to add, if you're refering to Master pages then a Link tag within the
    Head section gets resolved before being combined with its content page so
    "style.css" should work in your case (also link tags within conditional
    comments - thanks Steven). I've just read "ASP.Net 2.0 - Master Pages:
    Tips, Tricks, and Traps" by K. Scott Allen
    (http://odetocode.com/Articles/450.aspx) which is a great article on the
    subject.

    Hope that helps.

    Best regards

    John

    "Lance Wynn" <> wrote in message
    news:...
    > Hello all,
    > Is there a way to use the ~/ path to link a CSS page?
    >
    > I thought maybe something like this would work, but no go:
    > <link href="~/style.css" rel="stylesheet" type="text/css"/>
    >
    > Is this possible to do with the link element?
    >
    > Thanks
    >
     
    John, Jan 17, 2008
    #5
  6. Lance Wynn

    Leon Mayne Guest

    "Lance Wynn" <> wrote in message
    news:...
    > Hello all,
    > Is there a way to use the ~/ path to link a CSS page?
    >
    > I thought maybe something like this would work, but no go:
    > <link href="~/style.css" rel="stylesheet" type="text/css"/>
    >
    > Is this possible to do with the link element?


    Adding runat="server" should do it:
    <link href="~/style.css" rel="stylesheet" type="text/css" runat="server" />
     
    Leon Mayne, Jan 17, 2008
    #6
  7. Lance Wynn

    Hans Kesting Guest

    marss brought next idea :
    > On 17 Січ, 05:08, "Lance Wynn" <> wrote:
    >> Hello all,
    >> Is there a way to use the ~/ path to link a CSS page?
    >>
    >> I thought maybe something like this would work, but no go:
    >> <link href="~/style.css" rel="stylesheet" type="text/css"/>
    >>

    >
    > If you use href="/style.css" it refers to a stylesheet resided in the
    > root folder of an application
    > disregarding of position of the parent page in a website hierarchy.
    > IMHO, no need to convert the stylesheet link to a server control, a
    > leading slash "/" for HTML elements do the same as "~/" for web/HTML
    > controls.
    >
    > Regards,
    > Mykola
    > http://marss.co.ua - Casual ideas for web development


    That will work for a production server where the root of the
    application is http://www.company.com.
    It will fail on your development box where you want to use
    http://localhost/CompanyDevSite

    The ~ syntax (when used correctly) will work in both cases.

    Hans Kesting
     
    Hans Kesting, Jan 17, 2008
    #7
  8. Lance Wynn

    marss Guest

    On 17 Січ, 11:44, Hans Kesting <> wrote:

    > It will fail on your development box where you want to usehttp://localhost/CompanyDevSite


    You are right, I completely forgot this aspect.
    Perhaps, I became too spoilt by VS 2005 using http://localhost:1720/
    instead of http://localhost/MySite/ :)

    Regards,
    Mykola
    http://marss.co.ua - Casual ideas of web development
     
    marss, Jan 17, 2008
    #8
  9. Lance Wynn

    Patrice Guest

    Another option is to place the stylesheet in the site theme folder. It will
    be then automatically referenced from your pages...

    --
    Patrice

    "Lance Wynn" <> a écrit dans le message de news:
    ...
    > Hello all,
    > Is there a way to use the ~/ path to link a CSS page?
    >
    > I thought maybe something like this would work, but no go:
    > <link href="~/style.css" rel="stylesheet" type="text/css"/>
    >
    > Is this possible to do with the link element?
    >
    > Thanks
    >
     
    Patrice, Jan 17, 2008
    #9
  10. Lance Wynn

    Tahir Guest

    a problem with ~/

    hi,

    here is some code from my MasterPage:
    ......
    <body>
    <form id="form1" runat="server">

    <div id="main-header" class="box">
    <img src="Tema/banner.bmp" alt=""/>
    ......

    i have a "Tema" folder at root directory containing MasterPage and
    banner.bmp.
    * when i use this code, VS indicates that banner file is missing but at
    debug time web browser shows the image, the result is ok.
    * when i write there ="~/Tema/banner.bmp", VS says ok but browser says image
    missing where it shows the address "localhost/website/~/Tema/banner.bmp"
    * when i show the image by vs gui, the code apperas as
    src="../Tema/banner.bmp", image is still missing and browser shows the
    address "localhost/Tema/banner.bmp"

    can anybody who understands the "~" operator, describes this?


    "Lance Wynn" <>, iletisinde þunu yazdý,
    news:...
    > Hello all,
    > Is there a way to use the ~/ path to link a CSS page?
    >
    > I thought maybe something like this would work, but no go:
    > <link href="~/style.css" rel="stylesheet" type="text/css"/>
    >
    > Is this possible to do with the link element?
    >
    > Thanks
    >
     
    Tahir, Jan 17, 2008
    #10
  11. Re: a problem with ~/

    "Tahir" <> wrote in message
    news:...

    > can anybody who understands the "~" operator, describes this?


    The tilde (~) is server-side notation, not client-side notation...

    That is why something like: <img src="~/Tema/banner.bmp" alt=""/> can never
    work...

    However, <img src="~/Tema/banner.bmp" alt="" runat="server" /> will work...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Jan 17, 2008
    #11
  12. Howdy,

    Workaround 4:
    <link href="<%=ResolveClientUrl("~/Css/style.css") %>" type="text/css" />
    ResolveClientUrl method is invented for such purposes.
    --
    Milosz


    "Jialiang Ge [MSFT]" wrote:

    > Hello,
    >
    > From your post, my understanding on this issue is: you wonder how to
    > resolve the root directory when we link to a style sheet. If I'm off base,
    > please feel free to let me know.
    >
    > According to the MSDN article: ASP.NET Web Site Paths
    > http://msdn2.microsoft.com/en-us/library/ms178116.aspx
    > "ASP.NET includes the Web application root operator (~), which you can use
    > when specifying a path in *server controls*. ASP.NET resolves the ~
    > operator to the root of the current application. You can use the ~ operator
    > in conjunction with folders to specify a path that is based on the current
    > root."
    > Therefore, as Cowboy replied, the link is not attached to a server control,
    > and the operator '~' cannot be resolved as expected.
    >
    > Here are two workarounds for your reference:
    >
    > Workaround 1: Declare 'runat=server' for the <head> element, and use '~'
    > inside it. For instance,
    > <head runat="server">
    > <title>Untitled Page</title>
    > <link href="~/Css/style.css" rel="stylesheet" type="text/css" />
    > </head>
    >
    > Workaround 2:
    > We can link to the style sheet with some server side codes
    > (quoted from
    > http://www.pluralsight.com/blogs/ted/archive/2005/08/31/14437.aspx)
    > public static void AddLinkedStyleSheet(Page page, string stylesheet)
    > {
    > HtmlLink link = new HtmlLink();
    > link.Href = page.ResolveUrl(stylesheet);
    > link1.Attributes["text"] = "text/css";
    > link1.Attributes["rel"] = "stylesheet";
    >
    > page.Header.Controls.Add(link);
    > }
    > MyClass.AddLinkedStyleSheet(this, "~/css/my.css");
    >
    > Please let me know if you have any other concerns, or need anything else.
    >
    > Regards,
    > Jialiang Ge (, remove 'online.')
    > Microsoft Online Community Support
    >
    > ==================================================
    > For MSDN subscribers whose posts are left unanswered, please check this
    > document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx
    >
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications. If you are using Outlook Express/Windows Mail, please make sure
    > you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
    > see your reply promptly.
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    > ==================================================
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
     
    Milosz Skalecki [MCAD], Jan 17, 2008
    #12
  13. Hello,

    Would you mind letting me know the result of the suggestions? If you need
    further assistance, feel free to let me know. I will be more than happy to
    be of assistance.

    Have a great day!

    Regards,
    Jialiang Ge (, remove 'online.')
    Microsoft Online Community Support

    =================================================
    When responding to posts, please "Reply to Group" via your newsreader
    so that others may learn and benefit from your issue.
    =================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Jialiang Ge [MSFT], Jan 21, 2008
    #13
  14. Lance Wynn

    Lance Wynn Guest

    Sorry it took so long to get back to you, My wife went into labor, and I've
    been at the hospital until today. (Healthy boy, 11 lbs 13 oz aprox 5.3
    kilos, he is huge ;-)

    I was able to get it working using the runat=server in the <head> tag, I
    have not been able to try any of the other suggestions, but I certainly
    learned a lot more about ASP.net through this.

    Thanks alot,
    Lance



    "Jialiang Ge [MSFT]" <> wrote in message
    news:...
    > Hello,
    >
    > Would you mind letting me know the result of the suggestions? If you need
    > further assistance, feel free to let me know. I will be more than happy to
    > be of assistance.
    >
    > Have a great day!
    >
    > Regards,
    > Jialiang Ge (, remove 'online.')
    > Microsoft Online Community Support
    >
    > =================================================
    > When responding to posts, please "Reply to Group" via your newsreader
    > so that others may learn and benefit from your issue.
    > =================================================
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
     
    Lance Wynn, Jan 21, 2008
    #14
  15. Hello Lance,

    I'm very pleased to hear the good news. Congratulations! : )

    Regards,
    Jialiang Ge (, remove 'online.')
    Microsoft Online Community Support

    =================================================
    When responding to posts, please "Reply to Group" via your newsreader
    so that others may learn and benefit from your issue.
    =================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Jialiang Ge [MSFT], Jan 22, 2008
    #15
  16. Lance Wynn

    G.Lindqvist Guest

    On Jan 21, 5:03 am, "Lance Wynn" <> wrote:
    > Sorry it took so long to get back to you, My wife went into labor, and I've
    > been at the hospital until today.  (Healthy boy, 11 lbs 13 oz aprox 5.3
    > kilos, he is huge ;-)
    >
    > I was able to get it working using the runat=server in the <head> tag, I
    > have not been able to try any of the other suggestions, but I certainly
    > learned a lot more about ASP.net through this.
    >
    > Thanks alot,
    > Lance
    >
    > "Jialiang Ge [MSFT]" <> wrote in messagenews:...
    >
    >
    >
    > > Hello,

    >
    > > Would you mind letting me know the result of the suggestions? If you need
    > > further assistance, feel free to let me know. I will be more than happy to
    > > be of assistance.

    >
    > > Have a great day!

    >
    > > Regards,
    > > Jialiang Ge  (, remove 'online.')
    > > Microsoft Online Community Support

    >
    > > =================================================
    > > When responding to posts, please "Reply to Group" via your newsreader
    > > so that others may learn and benefit from your issue.
    > > =================================================
    > > This posting is provided "AS IS" with no warranties, and confers no
    > > rights.- Hide quoted text -

    >
    > - Show quoted text -


    That solution should work. If you don't wan't runat on the head tag
    you could add only "runat" on the link tag.

    This should also work, I think.

    <head>
    <link id="Link1" href="~/Content/main.css" rel="stylesheet"
    type="text/css" runat="server" />


    Regards,
    Gustaf Lindqvist
     
    G.Lindqvist, Jan 22, 2008
    #16
  17. Lance Wynn

    Lance Wynn Guest

    I thought this should work too, but it didn't. Apparently the link tag
    doesn't accept the runat=server parameter

    "G.Lindqvist" <> wrote in message
    news:...

    That solution should work. If you don't wan't runat on the head tag
    you could add only "runat" on the link tag.

    This should also work, I think.

    <head>
    <link id="Link1" href="~/Content/main.css" rel="stylesheet"
    type="text/css" runat="server" />


    Regards,
    Gustaf Lindqvist
     
    Lance Wynn, Jan 22, 2008
    #17
  18. Lance Wynn

    Lance Wynn Guest

    Thanks Patrice,
    I started studying about themes, skins, and dynamically setting the master
    page, and I am really liking where things are going. I think this will solve
    a lot of my ills.

    Thanks again for the helpful nudge!

    "Patrice" <http://www.chez.com/scribe/> wrote in message
    news:...
    > Another option is to place the stylesheet in the site theme folder. It
    > will be then automatically referenced from your pages...
    >
    > --
    > Patrice
    >
    > "Lance Wynn" <> a écrit dans le message de
    > news: ...
    >> Hello all,
    >> Is there a way to use the ~/ path to link a CSS page?
    >>
    >> I thought maybe something like this would work, but no go:
    >> <link href="~/style.css" rel="stylesheet" type="text/css"/>
    >>
    >> Is this possible to do with the link element?
    >>
    >> Thanks
    >>

    >
    >
     
    Lance Wynn, Jan 23, 2008
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy
    Replies:
    1
    Views:
    7,717
    Brian Lowe
    Jun 24, 2004
  2. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    867
    Kevin Spencer
    May 17, 2005
  3. Mupota Muchelemba
    Replies:
    1
    Views:
    920
    Tony Morris
    Feb 4, 2004
  4. Carl Gilbert

    Link to run script and link to page

    Carl Gilbert, Jan 13, 2004, in forum: Javascript
    Replies:
    3
    Views:
    130
    Michael Winter
    Jan 13, 2004
  5. pavi
    Replies:
    0
    Views:
    1,345
Loading...

Share This Page