HttpContext.Current.Server未将对象引用到实例

问题描述:

在一些类库中需要读取当前系统的xml文件,当时用HttpContext.Current无法找到实例化对象

解决代码如下:

XmlDocument xml = new XmlDocument();
//加载xml文件
try
{
if (HttpContext.Current != null)
{
xml.Load(HttpContext.Current.Server.MapPath("~/Configs/CityAreas.xml"));
}
else
{
xml.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs\CityAreas.xml"));
}

}
catch (Exception ex)
{
Helper.ComHelper.CityDataLog("CityAreas配置有误,请及时检查!");
}

原文地址:https://www.cnblogs.com/pangzili/p/8870516.html