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

情到浓时终转凉″ 提交于 2019-12-03 16:46:34

问题


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?


回答1:


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

See also this post.




回答2:


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");


来源:https://stackoverflow.com/questions/3549667/how-do-you-determine-the-physical-path-of-a-file-without-an-httpcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!