简体   繁体   中英

How do you determine the physical path of a file without an HttpContext?

I have some processes that run without an HttpContext in an ASP.NET MVC web application. This process needs to be able to determine the physical path to the Contents directory of the application for reading/writing data. But, since it is without an HttpContext, I don't get to use fancy things like Server.MapPath and such. Suggestions?

In a website, it is best to use HttpRuntime.AppDomainAppPath , because in certain moments of the execution path (ie when the site starts up), there's no HttpContext.Current available.

See also this post .

The best way to do this is using the AppDomain.BaseDirectory property. As long as you don't fiddle with custom application domains, it will point to your root application directory. In other words; these two string would be the same:

string mapUsingAppDomain = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Contents");
string mapUsingServer = HttpContext.Current.Server.MapPath("~/Contents");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM