关于"未能映射路径"问题

未能映射路径,在作页面生成时,老是出现"未能映射路径"/aa/bb/cc".

研究了半天,终于找出原因了,Server.Mapth(string path),path-->是相对路径。所以,改为Server.Mapth("aa/bb/cc")就好了,.net 会自动找"aa/bb/cc",返回相对路径。

if (!System.IO.Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
{
System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(path));
}
System.IO.StreamWriter sw = new System.IO.StreamWriter(System.Web.HttpContext.Current.Server.MapPath(path + "/" + file), false, System.Text.Encoding.GetEncoding("gb2312"));
sw.Write(temp);

如果不存在,使用System.IO.Directory.CreateDirectory创建文件夹。

切记: Server.Mapth("相对路径").

解决方案一:将绝对路径/bin/WebSet.xml设为相对路径即可:~/aa/bb/WebSet.xml

解决方案二:使用System.Web.HttpContext.Current.Request.PhysicalApplicationPath+("/Bin/WebSet.xml");
其中System.Web.HttpContext.Current.Request.PhysicalApplicationPath表示的是项目的根目录。

解决方案三:aa/bb/WebSet.xml

////////----->

写一段读写文件的程序,使用System.Web.HttpContext.Current.Server.MapPath("/bin/WebSet.xml")
,不料却出现“未能映射路径”的错误,马上检查程序,感觉没有什么错误,于是乎网上搜,
找啊找,就是找不到解决方案。只有自己慢慢调试了。

解决方案一:将绝对路径/bin/WebSet.xml设为相对路径即可:~/bin/WebSet.xml

解决方案二:使用System.Web.HttpContext.Current.Request.PhysicalApplicationPath+("/Bin/WebSet.xml");

其中System.Web.HttpContext.Current.Request.PhysicalApplicationPath表示的是项目的根目录。

////////------>

ds.ReadXml(HttpContext.Current.Server.MapPath("/Citys.xml"));

如果问题出来了:

未能映射路径“/Citys.xml”。

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: 未能映射路径“/Contacter.xml”。
把代码改成:

ds.ReadXml(HttpContext.Current.Server.MapPath("~/Citys.xml"));

///////

if (!IsPostBack)
{
string myStr = ConfigurationManager.ConnectionStrings["MapGuidingBusinessConnectionString"].ConnectionString.ToString();
SqlConnection myConn = new SqlConnection(myStr);
SqlDataAdapter adapter = new SqlDataAdapter("select * from OY_Location", myConn);
DataSet ds = new DataSet("markers");
adapter.Fill(ds, "marker");
string sXml = ds.GetXml();
string sFileName = Server.MapPath("Location.xml"); //假设你保存成xmlFile目录下b.xml
// Server.MapPath(@".\xmlFile\a.xml")
StreamWriter sr = File.CreateText(sFileName);
sr.WriteLine(sXml);
sr.Close();
}

原文地址:https://www.cnblogs.com/xinyuyuanm/p/2979570.html