我在 .aspx 页面上有一个超链接
<asp:HyperLink ID="hlTest" runat="server" NavigateUrl="#">Test Link</asp:HyperLink>
Run Code Online (Sandbox Code Playgroud)
在代码隐藏页面上,我有:
string link = "http://myDoman/myEmailAttachments/1436/" + HttpUtility.HtmlEncode("Picture of Jim&John.jpg");
hlTest.NavigateUrl = link;
Run Code Online (Sandbox Code Playgroud)
这会生成一个如下所示的网址: http://myDomain/myEmailAttachments/1436/Picture%20of%20Jim&John.jpg
这会导致显示一条消息:从客户端 (&) 检测到潜在危险的 Request.Path 值。
我曾尝试使用 Server.Urlencode。这会产生一个看起来像 ...
http://myDomain/myEmailAttachments/1436/Picture+of+Jim%26John.jpg
这会导致显示相同的消息:从客户端 (&) 检测到潜在危险的 Request.Path 值。
如果我有一个名为...
Jim&John的图片.jpg
...我怎样才能把它放到一个超链接中,这样它才能真正去获取文件?感谢您的任何帮助。
那是因为您不想进行 HTML 编码 ( HttpUtility.HtmlEncode),而是进行 URL 编码 ( HttpUtility.UrlEncode)。然后%26将被重写为&URL 的正确格式。这将防止 ASP.NET 将其视为潜在的恶意。
string link = "http://myDoman/myEmailAttachments/1436/"
+ HttpUtility.UrlEncode("Picture of Jim&John.jpg")
;
Run Code Online (Sandbox Code Playgroud)