Search code examples
asp.netiiswebformsvirtual-path

Is there a "symbol" on .NET that identify virtual-path?


Suppose this scenario. On my Default.aspx, I insert a context (WebForms), called MyContext.ascx, that load an image. So the path would be :

<img src="/images/hello.gif" />

Well. Now, I'd like to insert the same context on another .aspx page, that is in another forlder, such as /myfolder/MyPage.aspx

Than, the path of the image now should be :

<img src="../images/hello.gif" />

How you can see, I can't manage two different path for the same context. So, is there a way (symbol) to call the virtual-path of my application? Without using my own function as

<img src="<%=MyUtilities.GiveVirtualPath%>/images/hello.gif" />

which is boring. Who know?


Solution

  • Put your images in the root of your web-site (and inside a resources/styles/themes folder). You can use the tilde ~ to indicate the root of your site. All your pages will refer to that. If you're using server side controls you do not even need to use the ResolvePath() method (in your example you should use it, if for example you wrote asp:image ImageUrl="" you do not need to. From MSDN.