1

I'm using url routing and I have a stylesheet that is being referenced on the destination page (inside a master page content template):

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="css/actionmenu.css" rel="stylesheet" type="text/css" />
</asp:Content>

When the page is requested www.mysite.com/mypage it is Ok. However, if the page is requested as www.mysite.com/mypage/anotherpage - the reference to the stylesheet breaks. I tried:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
        <link href="~/css/actionmenu.css" rel="stylesheet" type="text/css" runat="server"/>
    </asp:Content>

and that didn't help.

My usual solution is to load the stylesheet in codebehind - however, is there another solution I'm missing?

0

3 Answers 3

1

I believe that you can use Server.ResolveClientUrl() to handle this in ASP.NET:

href="<%=Server.ResolveClientUrl("~/css/actionmenu.css")%>"

In ASP.NET MVC, you could use the Url.Content() method:

href = <%=Url.Content("~/css/actionmenu.css")%>"

If you are looking for some additional information on these options, you can check the link below:

Different Approaches for Resolving URLs | A Programmer's Blog

Sign up to request clarification or add additional context in comments.

1 Comment

maybe petty, but did you have to put it all in bold? Good answer though.
0

Try this.

<link href='<%= ResolveUrl("~/css/actionmenu.css") %>' rel="stylesheet" type="text/css" media="all" />

Comments

0

Is there any reason you can't use an absolute reference - i.e

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.