Reputation: 1247
I know this may be a basic question, but it is driving me crazy. I have an asp.net (4.0 framework) application that I have been working on. I have a masterpage in the root directory, and a secondary masterpage in a subdirectory. The secondary masterpage inherits the site master.
The issue is that even though I used ~/ to describe the location of the resource ("<img src="~/Images/myImage.jpg" />) they are not loading.
Using the console on firebug, I get this error: "NetworkError: 404 Not Found - http://localhost:4601/Account/~/Images/myImage.jpg"
What do I need to do to correctly translate resources from masterpage to masterpage across subfolders? And what is it that I am misunderstanding about '~/'?
Upvotes: 3
Views: 1752
Reputation: 21112
<img src="<%= Page.ResolveUrl("~/Images/myImage.jpg") %>" />
or
<img src="<%= Control.ResolveUrl("~/Images/myImage.jpg") %>" />
Upvotes: 0
Reputation: 140993
Using
<img src="~/Images/myImage.jpg" />
Is mixing HTML code with .Net ASP code. The tilde (~) is not something of the HTML markup and this is why it does not produce what you want.
To make it works, you need to change the source with the <% %> tag that will let you add ASP code that will be translated into HTML code when processing.
<img src="<%= Page.ResolveUrl("~/Images/myImage.jpg") %>" />
Inside ASP.NET tag, you should use the ResolveURL that will transform the URL into something that the HTML will be able to understand.
If you do not want to use this trick, you can also use instead of the HTML img tag the ASP.NET image control. This will automatically execute the ResolveUrl
<asp:Image runat="server" ID="imgHelp" ImageUrl="~/Images/myImage.jpg" />
Upvotes: 12