【问题标题】:Error when pass a directory path传递目录路径时出错
【发布时间】:2013-11-02 18:32:03
【问题描述】:

在起始页中:我想在客户端单击链接按钮lbtnEditText时将目录路径传递给弹出窗口页面

<asp:LinkButton  ID="lbtnEditText" runat="server" Text="Edit Text" CommandArgument='<%# Eval("Path") + "," + Eval("Name")%>' OnCommand="Button1_Click"></asp:LinkButton>

以及背后的代码:

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

帮助!!!为什么我传给editpage的时候路径变了???

【问题讨论】:

标签: c# asp.net runtime-error


【解决方案1】:

发生这种情况的原因是 :: 您传递的查询字符串数据包含意外字符,即“\、#”。 解决方案是在设置为查询字符串值之前对这些值进行转义和编码

【讨论】:

    【解决方案2】:

    不幸的是,任何进行 Web 开发的人都需要正确编码 Urls...

    # 之后的所有内容都是 Url 的“哈希”部分,浏览器不需要将其发送到服务器。更正式的名字是fragment identifier

    您需要做的是正确编码 path 查询参数的值(即在 JavaScript 中使用 encodeURIComponent 函数)。

    【讨论】:

    • 我以前从不知道encodeURIComponent,你能举个例子吗?提前致谢
    【解决方案3】:

    为您提供 C# 的实际解决方案:

    string queryString = "editpage.aspx?path=" + System.Web.HttpUtility.UrlEncode(values[0]);
    

    参见编码参考http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx

    解码http://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode%28v=vs.110%29.aspx

    【讨论】:

    • 感谢您的解决方案和详细链接。这是一个很好的帮助。再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2016-12-23
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多