Search code examples
c#asp.netasp.net-mvc-5

ASP.NET MVC5 access folder outside project


I have two ASP.NET MVC5 applications: A and B. Both sites want to access a folder named C. This folder is outside these projects. So the folder structure is:

--/root

------A site files

------B site files

------C files wanted by both of the sites

(Folder C contains mostly images.)

So I want to display the images from folder C on site A and site B as well.

But if I set an img tag's source attribute to "/C/..." on site A, it means "/A/C/..." and of course, the file will not be found.

How could I reach my goal?

Thanks for help!:)


Solution

  • If you have site A as a Virtual Directory and site B as a Virtual Directory and both are in a web-application that is served as root for the domain. If that's the case, you can set the relative path to the root for resources like images like you mentioned.

    Setting a root-relative path starting with / will work, since it will point to the root. But if you run your application A using it's own domain (so not under root's domain) using a root-relative path will point to the root of application A, not to site B.

    You can test this by setting site A up as a virtual directory on your local IIS under the default web-application. Default web-application can be reached by http://localhost. If the virtual directory is called SiteA you can reach it with http://localhost/SiteA. If you use a roort-relative path starting with / it will always point to http://localhost. So in this setup you can reach any other location that's served under http://localhost and it's virtual directories, including SiteC (as a virtual directory).