2013-10-24 74 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 –