1
votes

I have a folder of files (form templates) that need to be accessed from both a .NET Web API application and a separate .NET MVC application. The folder for these form templates is outside of the websites. Ideally, I'd like to be able to store the path in the Web.Config files so that the applications can be easily tested in a local environment.

I tried setting up virtual directories on the hosted site but couldn't figure out how to reference them in the Web API. I tried several means of referencing the Virtual Directory that did not work. Most posts suggested trying Server.MapPath("MyVirtualDirectory") but that returns "....\wwwroot\MyApiController\Action\MyVirtualDirectory", not the physical path of the virtual directory.

I removed the Virtual Directory and attempted to "navigate" to the correct path but was blocked by "Cannot use a leading .. to exit above the top directory".

So what is the correct way to access a resource using a virtual directory in .NET Web API application? Is the same method going to work for the .NET MVC application?

1
Why don't you use the absolute url, starting with http? - ataravati

1 Answers

3
votes

You need to use HostingEnvironment, like:

public static string MapPath(string path){
    string result;

    result = HostingEnvironment.MapPath(path);

    return result;
}

Additionally HostingEnvironment provides features like ApplicationPhysicalPath:

result = HostingEnvironment.ApplicationPhysicalPath + "App_Data\\somefile.xml";