[RESOLVED]image is not displaying on each page


I have problem with displaying images inside text. For example I have text:

This is text with image <img src="~/content/images/image1.jpg" />

This text wil be showing well on home page http://mypage.com but when I want show this text on other page |(eg
http://mypage.com/products/product1) the image is not displaying.

Could you please help me what I’m doing wrong? 



What MVC version do you have?
Use also



thanks quick answer.

I’m using MVC 5.

@Url.Content is not an option in this situation. images are adding inside html editor by content administrator (I’m using Html Kendo Editor).

Text is editing on mysite.com/adinistrator/text_managment and can be displayed on many different pages (with diferent level of url: eg mypage.com/text, mypage.com/products/product1, mypage.com/shoppping_card/order/order_confirmation )



What is the generated HTML on the URL that works and what is the generated HTML on the URL that does not work ?

Html is the same. <img src="~/content/sites/18/images/image1.jpg" />

it is NOT  working on : htp://localhost:12345, htp://localhost:12345/page1, htp://localhost:12345/page2 etc

but it is working on  htp://localhost:12345/page1/, htp://localhost:12345/products/product1 etc

seems that path to "content" folder has to be fixed ("~" doesnt work or I’m using it in wrong way)



Are you sure that html is


<img src="~/content/sites/18/images/image1.jpg" />

That means that is not interpreted by Razor ? ( Hint: ~ is for server, not for the client)

In which file do you have this <img src ?

Are you sure that you use MVC5 ?

yes, in page source view is displaying with ~.

I’m using Html.Raw(text for display from db where is path to image <img src="~/content/sites/18/images/image1.jpg" />)

I’m sure that it is MVC5




~ is a server side notation (to note the "application root" rather than the "web site root"). As here you are generating the whole tag yourself it can’t be processed automatically by the MVC Framework so that it is resolved to the proper actual location.

You should be able to use http://stephenwalther.com/archive/2009/02/18/asp-net-mvc-tip-47-ndash-using-resolveurl-in-an-html to resolve this programmatically
before sending back the raw HTML. Or maybe just store in your db a virtual path that has been already resolved? Or if you are 100% your "application root" is at the "web site root" rather than inside a subfolder, then you could drop the additional ~ from your
db (and have this option when creating the actual raw HTML).

Leave a Reply