博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于ASP.NET"未能映射路径"问题
阅读量:6987 次
发布时间:2019-06-27

本文共 2285 字,大约阅读时间需要 7 分钟。

通过vs【新建项目】或者【新建网站】而创建的网站项目会使用"/aa/bb/cc"(以/开头)的相对路径,而通过【文件】》【添加】》【现有网站】建立的网站项目使用"aa/bb/cc"(不以/开头)的相对路径。

根路径 ../

参考文章:

 

未能映射路径,在作页面生成时,老是出现"未能映射路径"/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();
        } 

本文转自火地晋博客园博客,原文链接:http://www.cnblogs.com/yelaiju/archive/2012/02/10/2344830.html,如需转载请自行联系原作者

你可能感兴趣的文章
在Mac版本下的IDEA中设置代码注释模版
查看>>
我的友情链接
查看>>
Ruby实现二分法查找
查看>>
OA系统报错
查看>>
JavaScript设计模式之一:面向对象的Javascript
查看>>
Web 前沿技术:展示一组极其绚丽的 CSS3 效果
查看>>
云时代如何做好IT运维审计
查看>>
唐老师答疑
查看>>
第十二章 简单工厂模式(Simple Facotry)
查看>>
LeetCode - 70. 爬楼梯
查看>>
visualVm监控tomcat
查看>>
Jquery script for document preview?
查看>>
【Magedu】Week02
查看>>
写给MongoDB开发者的50条建议Tip12
查看>>
我的友情链接
查看>>
linux下查看nginx,apache,mysql,php编译命令
查看>>
JQUERY学习第三天之浮动和弹出窗口
查看>>
python中asynchat异步socket命令/响应处理
查看>>
动态编译
查看>>
linux下批量解压缩
查看>>