Search code examples
asp.net-mvciisvirtual-directory

URLs pointing to wrong virtual directory on IIS


I have an IIS web application called "WebApp" created inside the "Default Web Site". The WebApp has a virutal directory with a repository of images called "Repository".

Schema:
-Default Web Site
    -WebApp
        -Repository

When I execute the web application like this:

http://localhost/WebApp

All my images points to a relative url like "/Repository/ImageDir/image.jpg" which is interpreted like http://localhost/Repository/ImageDir/image.jpg.

That URL is wrong, because since my virtual directory is inside WebApp, the correct URL should be http://localhost**/WebApp**/Repository/ImageDir/image.jpg

How can I make it generate a correct URL.

Just for more info, the web application is in ASP.NET MVC.

UPDATE:

I'm using Razor in my views, so here is the code to generate a img tag:

<img src="@m.ThumbPath" />

Where "m" is an element of the Model (which is a List of elements) and "ThumbPath" contains the relative path that is giving problems.


Solution

  • Since you are using Asp.Net MVC you should be using Url.Content-helper to generate the url to resources inside your app:

    <img src="@Url.Content(String.Format("~{0}", m.ThumbPath))">