How to specify virtual path for image in ASP.Net MVC razor helper

OK, solved it, though I'm not really sure why it's working. After trying all the following combinations without success:

<img src="../Content/images/ajax_activity.gif" alt="loading"/>
<img src="/Content/images/ajax_activity.gif" alt="loading"/>
<img src="~/Content/images/ajax_activity.gif" alt="loading"/>
<img src="Content/images/ajax_activity.gif" alt="loading"/>

the following finally worked as expected

<img src="./Content/images/ajax_activity.gif" alt="loading"/>

It returned the image path correctly with the virtual directory set. Anyone able to explain this?


You could use @Url.Content method to convert virtual relative path to absolute application path like this:

<img [email protected]("~/images/picture.png") alt="picture description">

It'll be converted in this HTML code forwarded to client:

<img src="/appname/images/picture.png" alt="picture description">

UPDATE: In other hand you could convert image to base64 and it will be displayed correctly too:

<img src="data:image/png;base64,iVBORw0KG...SuQmCC" alt="picture description">

Try this:

<img src="@Url.Content("~/Content/images/ajax_activity.gif")" alt="loading" />