views:

17

answers:

2

I have an ASP.NET MVC web application running in IIS as a subweb; let's say its path is something like http://mysite.com/subweb.

I have a view which serves content obtained from a CMS. The CMS editor has entered some content containing a relative image path <img src="/images/imga.png" />.

This works fine on the production server where the site is the root website, but not on the staging server where the site is a virtual directory under the root website

The path should be <img src="/images/imga.png" /> on the production server

and <img src="/subweb/images/imga.png" /> on the staging server.

Is it possible to use the <base> tag to resolve this image path?


<head>
<base href="http://mysite.com/subweb/" />
</head>

<html>
  <body>
    <img src="/images/imga.png" />
  </body>
</html>


It doesn't seem to work. Can anyone explain why or if this is a workable approach? I don't want to require the content editor to have knowledge of the website deployment option (which changes between UAT and production).

A: 

Try

   <img src="images/imga.png" />

to make it a relative URL. Of course it's not that easy, the content editor must be changed. But I do believed that is the problem. URLs starting with a / are resolved from... (wonders) the domain's top-level directory?

MvanGeest
A: 

The base tag only affects relative URLs. Try changing your image tag to this:

<img runat="server" src="~/images/imga.png" />

This will work even in sub directories of your app. .Net will resolve the "~/" to your application root.

David
This is not a workable resolution because I can't depend on the content editor to always add the runat="server" attribute or the ~ character.
Keith
@Keith To avoid the runat="server" attribute, you can use <%Page.ResolveClientUrl("~/images/imga.png")%> instead.
Danny Chen