Search code examples
asp.netdirectoryvirtual

Access Virtual Directory in Another Web Application


I have two web application projects (project A and project B) in the same solution.

I want to be able to: through project A, save a file somewhere in the directory of project B.

Whenever I use Server.MapPath etc, it resolves to the virtual directory of project A, not of B.

From my reading, I believe I should be able to type the URL of project B directly into the save method. However, neither project is deployed at the moment. Besides, I want to leverage the fact that they are on the same solution.

TIA!


Solution

  • But if you're saving a file, then you don't want to access the virtual directory; you want the actual (physical) directory. That means Request.MapPath is the way to go; you can use the overload with cross-app mapping set to true.

    string pathToAppB = Request.MapPath("/AppBVirtualPath", Request.ApplicationPath, true);
    

    You still have to know which virtual path application B will be deployed to, though; and you can't test locally unless you deploy to your local IIS.