[博客迁移][C#] Server.MapPath 未将对象引用设置到对象的实例

228 阅读1分钟

from C# Server.MapPath 未将对象引用设置到对象的实例


System.Web.HttpContext.Current.Server.MapPath(string sfilePath)将虚拟路径转换成物理路径。这个必须在aspx或者MVC中Action调用才行,即必须是有HttpContext.Current对象。在线程执行任务中若是调用了System.Web.HttpContext.Current.Server.MapPath(string sfilePath)也会报异常,因为没有HttpContext.Current对象。

解决方法:使用System.AppDomain.CurrentDomain.BaseDirectory(应用程序根路径)拼接路径