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

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

阅:经验证,可行;

摘自:http://archive.cnblogs.com/a/1905813/

作者:露水丛生 来源:博客园 发布时间:2010-12-14 16:45 阅读:19 次 原文链接 [收藏]


未能映射路径,在作页面生成时,老是出现"未能映射路径"/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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值