C# ASP.NET HttpWebRequest 自动解码查询字符串中的符号 (&) 值?

标签 c# asp.net query-string decode ampersand

假设以下网址:

"http://server/application1/TestFile.aspx?Library=Testing&Filename=Documents & Functions + Properties.docx&Save=true"

我使用 HttpUtility.UrlEncode() 对 Filename 参数的值进行编码,并创建以下 Url:

"http://server/application1/TestFile.aspx?Library=Testing&Filename=Documents%20%26%20Functions%20%2B%20Properties.docx&Save=true"

我将以下请求(编码版本)从客户端发送到 C# Web 应用程序。当我处理请求时在服务器上遇到问题。 HttpRequest 变量包含部分解码的查询字符串。也就是说,当我尝试使用或快速查看 HttpRequest 的以下属性时,它们具有以下值。

Property = Value
================
HttpRequest.QueryString = "{Library=Testing&Filename=Documents+&+Functions+++Properties.docx&Save=true}"

HttpRequest.Url = "{http://server/application1/TestFile.aspx?Library=Testing&Filename=Documents & Functions + Properties.docx&Save=true}"

HttpRequest.Url.AbsoluteUri = "http://server/application1/TestFile.aspx?Library=Testing&Filename=Documents%20&%20Functions%20+%20Properties.docx&Save=true"

我还检查了以下属性,但所有属性的 & 值都已解码。然而,所有其他值都保持正确编码(例如,空格是 %20)。

HttpRequest.Url.OriginalString

HttpRequest.Url.Query

HttpRequest.Url.PathAndQuery

HttpRequest.RawUrl

我无法正确读取参数文件名的值。我错过了什么吗?

最佳答案

QueryString 属性返回一个 NameValueCollection 对象,它将查询字符串键映射到完全解码的值。

您需要编写 Request.QueryString["FileName"]

关于C# ASP.NET HttpWebRequest 自动解码查询字符串中的符号 (&) 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3667902/

相关文章:

c# - 所选日期未显示在 angular.js 的日期选择器中

c# - ASP.NET Core在提交表单时排除viewmodel的属性

c# - 即使似乎满足所有条件,JIT 编译器也不会内联方法

c# - 在配置中指定条件跟踪监听器

ruby-on-rails - 在设计注册中保留查询字符串

c# - 在 asp.net mvc 中按查询字符串搜索

c# - 增加 QueryString 最大长度

c# - 是否可以将字符串用于 LINQ 查询表达式?

C# - : String. Contains() 或 Regex.isMatch() 哪个更快?

c# - Asp.Net Ajax WebService 和内部错误 500