19

The problem

I'm running into the typical virtual-directory dilemma in that you have some paths on your ASP.Net application and you deploy the app in a IIS virtual directory. Then all paths relatives to the "web root" (f.i., "/images") doesn't work because the app is in a virtual directory path.


The solutions

A. Make the "images" folder a virtual directory. This way "/images" will always exist.

B. Use "<%=Request.ApplicationPath%>/Imagenes" as the source of my images. This works great in IIS but I can't see it in design-time nor in debug-time.

This solution also include these instructions:

  • System.Web.VirtualPathUtility.ToAbsolute
  • ResolveClientUrl
  • Request.ApplicationPath

C. Use relatives paths to the current control/page. This is know exactly where the images folder is relative to my current file (without go to the root. So I would use things like "", "../", "../../" and so on.


The solution I'm looking for

Said that. I don't like these solutions. I would want a solution in the web.config file or in IIS. Some conf intruction I write in the web.config file that tells IIS where my application resides actually (virtual directory).

Any advice?

David Elizondo
  • 1,123
  • 1
  • 7
  • 16

2 Answers2

12

Are you using the tilde (~) for your paths where you can?

~ refers to the root of the virtual Web application....

~/images for example.

Kevin LaBranche
  • 20,908
  • 5
  • 52
  • 76
  • 1
    alternatively, for non-server-side img elements, you can use the "/" character to refer to the root, as in . – David Andres Sep 19 '09 at 19:44
  • 5
    @klabranche, the tilde (~) is for server-side controls and only usable in server-side code (http://msdn.microsoft.com/en-us/library/ms178116.aspx) @DavidAndres, The slash "/" assumes your app is in the Web Site root (not for virtual directories). (http://msdn.microsoft.com/en-us/library/ms178116.aspx) > A site-root relative path, which is resolved against the site root (not the application root). – David Elizondo Sep 19 '09 at 20:08
  • 1
    @dealmo - I did say where you can.... I understand you are looking for an alternate approach to the one's you listed. I was just making sure you have at least done what you can where you can. :) – Kevin LaBranche Sep 19 '09 at 20:15
  • @klabranche - sorry, and thanks. In fact I used it in a couple of controls. But I want to resolve it in aspx pages and css files :/ – David Elizondo Sep 19 '09 at 20:23
  • @daelmo: You're correct. The images either need to be placed in inetpub\wwwroot\images or the src attribute of the img element should be updated to "/virtual directory/images/back.png" Thanks for the update. – David Andres Sep 19 '09 at 20:50
  • And there is no solution **what-so-ever** if I can't change my urls (because that would mess up when uploaded to production)? – marquito Apr 03 '13 at 20:07
  • I am using ~/App_Themes already, but it still resolve to the app root, not the virtual directory root, any idea? – Zennichimaro Apr 28 '14 at 10:31
3

If it's just for css files on the client side then using the url directive makes the path relative to that of the style sheet rather than the page:

h1#title { background: url('dog.gif') no-repeat 0 0; } 

Also if you're on asp.net mvc then you have access to:

<script src="<%= Url.Content("~/scripts/new.js") %>" type="text/javascript"></script>
Giles Roberts
  • 6,407
  • 6
  • 48
  • 63
  • I use this solution without any path in combination with „Themes and Skins“, placing the images in the theme directory side by side with the stylesheet. – Dirk Brockhaus Sep 28 '12 at 12:08