How to tackle url/href/css/js when in virtual directory?

Posted by Community Admin on 03-Aug-2018 16:49

How to tackle url/href/css/js when in virtual directory?

All Replies

Posted by Community Admin on 24-Aug-2011 00:00
I ran into a problem where I needed to deploy Sitefinity website in a virtual directory. I came to know that my stylesheet/js links doesnt work with following:

<head runat="server">
<link href="/Common/CSS/Default_en_gb.css" rel="stylesheet" type="text/css" />
<script src="/Common/JS/myJSFile.js" type="text/javascript"></script>
</head>

I cant use <%= BaseUrl %> to manage the urls when as a independent website or in virtual directory.

Please suggest.

Thank you
Posted by Community Admin on 24-Aug-2011 00:00
Have you tried using ResolveUrl()?

Something like this should work:

<link href="<%= ResolveUrl("~/Common/CSS/Default_en_gb.css") %>" rel="stylesheet" type="text/css" />

There is also VirtualPathUtility.ToAbsolute(), although I have never tried it.

Finally you can also use the Sitefinity controls for this: Sitefinity Designer's Guide: Including JavaScript and CSS Resources

hope this is helpful!
Posted by Community Admin on 29-Aug-2011 00:00
Hi Saad,

In addition to the previous suggestions you can try to put the path to css or .js file in the browser and see if they are accessible. If you are forbidden to view them check if you have proper permissions on the folders where css and .js files reside.

Best wishes,
Stanislav Velikov
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Posted by Community Admin on 01-Sep-2011 00:00
Thanks SelAromDotNet for the suggestion!

I went deep into the control that Sitefinity offers and tried that way. I used ResourceLinks control to have my stylesheets referenced to my masterpages correctly.

<sitefinity:ResourceLinks id="resourcesLinks" runat="server">
</sitefinity:ResourceLinks>

Then I added resources on runtime as needed (for English and Arabic):

Telerik.Sitefinity.Web.UI.ResourceFile resource = new Telerik.Sitefinity.Web.UI.ResourceFile();
            resource.Name = "Styles/Default_" +
                            (Request.Url.ToString().ToLower().Contains("/ar/") ? "ar_ae" : "en_gb") +
                            ".css";
 
            resourcesLinks.Links.Add(resource);

Thanks very much for the guidance guys.
This thread is closed