Search code examples
asp.netvirtualrelative-pathvirtual-directory

Can I use an HTML base tag to resolve a virtual directory?


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).


Solution

  • 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?