2013-10-24 67 views
1

在起始頁錯誤時傳遞的目錄路徑

protected void Button1_Click(object sender, CommandEventArgs e) 
    { 
     string[] values = e.CommandArgument.ToString().Split(','); 

     string queryString = 

      "editpage.aspx?path=" 

      + values[0]; 

     string newWin = 

      "window.open('" + queryString + "');"; 

     ClientScript.RegisterStartupScript 

      (this.GetType(), "pop", newWin, true); 

    } 

queryString究竟是= "editpage.aspx?path=D:\\C#Projects\\website\\Lecturer\\giangvien\\profile.xml"(我在調試時檢查它)

但是在目標頁面(彈出窗口)中:editpage.aspx

string path = Request.QueryString["path"]; 
    string content = File.ReadAllText(path); 
    if(content!=null) 
     textarea.Value = content; 

它有一個錯誤:Could not find file 'D:\C#Projects\website\C 嘗試調試,我收到了path僅僅只是:"D:C"

而且在editpage.aspx顯示的地址欄:

http://localhost:41148/website/editpage.aspx?path=D:C#ProjectswebsiteLecturergiangvienprofile.xml

幫助!爲什麼當我將它傳遞給編輯頁面時路徑發生了變化?

+0

看看URL編碼的 –

+1

可能重複[如何編碼包含哈希?](http://stackoverflow.com/questions/9319656/how-to-encode-a-path-that-c​​ontains-a-hash) –

回答

2

發生這種情況的原因是::你傳遞的查詢字符串數據有'\,#'等意外字符。 的解決方案,這是逃避,設置爲查詢字符串編碼前值這個值

2

網址進行編碼的人做Web開發正確不幸所需的技能...

#後一切都是「哈希」 URL的一部分瀏覽器不需要將其發送到服務器。更正式的名字是fragment identifier

您需要做的是正確編碼path查詢參數的值(即使用JavaScript中的encodeURIComponent函數)。

+0

我從來不知道encodeURIComponent之前,你可以給我一個例子樂。在此先感謝 –

+0

@VyClarks http://www.bing.com/search?q=encodeURIComponent –