zszep zszep - 1 year ago 214
CSS Question

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

In my razor style helper class (located in the App_Code folder, I've got this line of code:

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

This works fine in Cassini, but when I deploy the app to IIS (virtual directory), IIS can't find the path. The virtual path is being ignored.
This also doesn't work:

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

Answer Source

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?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download