關於ASP.NET"未能映射路徑"問題


通過vs【新建項目】或者【新建網站】而創建的網站項目會使用"/aa/bb/cc"(以/開頭)的相對路徑,而通過【文件】》【添加】》【現有網站】建立的網站項目使用"aa/bb/cc"(不以/開頭)的相對路徑。

根路徑 ../

參考文章:http://www.cnblogs.com/lushuicongsheng/archive/2010/12/14/1905813.html

 

未能映射路徑,在作頁面生成時,老是出現"未能映射路徑"/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();
        } 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM