tags:

views:

43

answers:

2

Hi.

For example I have site http://localhost/site In IIS I set that 404 error causes redirection to default.aspx If I type something like http://localhost/site/nodirectory , (there are no such folder) all works perfectly.

But if I only add slah at end http://localhost/site/nodirectory/, page can't display css and images.

Images and css are located in their own folder. I tried different paths: "gfx/logo.gif", "/gfx/logo.gif"

Does anyone have some ideas about that?

A: 

There are a couple of options...

1)

In your HTML page, make the path to CSS and scripts relative...

"/scripts/myscript.js"

Where the scripts folder is the first folder after the root folder

2)

You can add the base tag to your page, which means ALL page resources will be treated as relative to the root location you specify...

<base href="http://www.mysite.com"&gt;

More info about these two options.

If you can, option 1 is perhaps a bit cleaner. You know explicitly the resources that you are affecting. Using the base tag will affect ALL relative paths on your page. Images, Links, Scripts, CSS et al. The second option works best if you developed your 404 page assuming it would be in the root folder, but it could actually be referenced from any non-existent directory. You just put your root address in the base tag and it will all behave exactly as you expect.

With either option, the images can be relative to the location of your CSS file.

Sohnee
+4  A: 

If your css and images are relative paths, say ResolveClientUrl("~/gfx/logo.gif") this renders to the client as src="gfx/logo.gif", which the browser with a slash thinks is /nodirectory/gfx/logo.gif instead of just /gfx/logo.gif.

To resolve this, don't use .ResolveClientUrl(), use .ResolveUrl(), this will make the src render src="/gfx/logo.gif" The beginning / makes it definitive, it's that path from the root of the domain.

You'll see this same hebavior if you're doing paths that start with ../ or gfx/ yourself...make them relative to the application base so there's no chance of confusion.

Nick Craver