0

I have an ASP.NET web app hosted in a windows dedicated server. In my app there are many folders. To simplify my problem let's say I have this scenario:

  • folder
    • default.aspx
    • css
    • js

I would like to setup default.aspx as my default page, meaning, when a user types domain.com, default.aspx is shown.

In order to do this, I edited my web.config file and it works. The problem is styles and javascripts.

My default.aspx contains this:

<script type="text/javascript" src="js/xxxx.js"></script>
<link href="css/styles.css" rel="stylesheet" type="text/css" />

So the styles and javascripts aren't found.

I really don't want to remove the folder and put everything in the root, and just moving the default.aspx page is not really an option, as I have a MasterPage.

Gonzalo
  • 982
  • 6
  • 23
  • 39

4 Answers4

0

use resolveUrl or a path from root to resolve locations of js and css

    <script src='<%= this.ResolveUrl("~/js/xxxx.js") %>' type="text/javascript"></script>
    <link href="~/css/styles.css" rel="stylesheet" type="text/css" />
DotNetUser
  • 6,494
  • 1
  • 25
  • 27
  • @DoNetUser ResolveURL doesn't seem to work in stylesheets. The html shows this: . Root relative path works, the only problem is when working locally, it doesn't work as the path also contains the name of the Web Application. – Gonzalo Mar 16 '12 at 01:46
  • can you do a ResolveUrl for it from the code behind if possible n check to see if you are using runat="server" in element. This link can be useful http://stackoverflow.com/questions/2588740/resolveurl-problem-in-master-page – DotNetUser Mar 16 '12 at 02:04
  • Page.ResolveUrl("~//css/styles.css"); returns "/DTSOnline/css/styles.css", which is correct when working locally. I've checked and element contains the runat="server" attribute – Gonzalo Mar 16 '12 at 16:28
0

If you're wanting to link to items, you can use the path from the website root. Just place a slash in front of the path.. like this:

 <link rel="stylesheet" href="/css/webedit.css" type="text/css" />
divamatrix
  • 1,126
  • 1
  • 7
  • 21
  • Root relative path works OK on server, the problem is when working locally as the path also contains the name of the Web Application. – Gonzalo Mar 16 '12 at 01:50
0

You could use Page.ResolveUrl method to get correct paths:

<script type="text/javascript" src='<%= Page.ResolveUrl("~/js/xxxx.js") %>'></script>
<link href='<%= Page.ResolveUrl("~/css/styles.css")' rel="stylesheet" type="text/css" />

For more info see Specifying Paths for Resources.

EDIT: it's mentioned in the comment that this is not working for stylesheets. It's partially true. It won't work for server-side, but will for client-side elements.

It seems your <link> element is located inside a server-side <head> tag, this means ASP.NET treats <link> inside <head> as a server-side controls even if you didn't specify runat="server" attribute there. So you don't need to use a server-side construct in that case:

<link href="~/css/styles.css" rel="stylesheet" type="text/css" />
Community
  • 1
  • 1
Oleks
  • 31,955
  • 11
  • 77
  • 132
0

For ASP.NET WebForms and MVC projects I strongly reccomend moving to Client Dependency framework and let it handle your page dependencies.

For your two dependencies you simply add these declarations to the page:

<CD:CssInclude ID="CssIncludeStyles" runat="server" FilePath="~/css/styles.css" />
<CD:JsInclude ID="JsIncludeXXXX" runat="server" FilePath="~/js/xxx.js" />

This is already cleaner then calling this.ResolveUrl on each dep. declaration.

You can furthermore facilitate the declaration of dependencies by introducing mapped paths in the dependency framework (see documentation for how-to).

An easiest way to start with client dependency is to add it via NuGet to your ASP.NET web site project.

It ensures you never have duplicate dependencies on one page and gives you a control over how dependencies are ordered.

Maxim V. Pavlov
  • 10,303
  • 17
  • 74
  • 174