开发者

while using asp.net url routing what is a suggested solution to referencing css files?

开发者 https://www.devze.com 2023-03-28 06:09 出处:网络
I\'m using url routing and I have a stylesheet that is being referenced on the destination page (inside a master page content template):

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="he开发者_开发问答ad" 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?


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


Try this.

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


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

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

精彩评论

暂无评论...
验证码 换一张
取 消