c# - .NET UrlEncode 不工作?

标签 c# asp.net

根据 http://www.w3schools.com/tags/ref_urlencode.asp 当请求被提交时,它们是 URL 编码的,因此例如空间被转换为 %20。到目前为止一切顺利。

我对 ! 有疑问。以表单形式提交会将其转换为 %21。 但是 HttpUtility.UrlEncode(或其 WebUtility 伙伴)或 Uri.EscapeDataString 都会返回! 这是预期的行为吗?我应该如何对来自 C# 的输入进行编码,以便将其转换为正确的值?

最佳答案

感叹号被认为是 URL 安全的 ASCII 字符,因此不是百分比编码。

来自 MSDN

The UrlEncode method URL-encodes any character that is not in the set of ASCII characters that is considered to be URL-safe. Spaces are encoded as the ASCII "+" character. URL-safe ASCII characters include the ASCI characters (A to Z and a to z), numerals (0 to 9), and some punctuation marks. The following table lists the punctuation marks that are considered URL-safe ASCII characters.

表格包含- _。 ! * ( )

更新

根据 this answer , Uri.EscapeDataString 应该在针对 .NET 4.5 项目时对 ! 进行编码,但我无法在我当前的机器上对其进行测试。以前的 .NET 框架上的 EscapeDataString 不会对上述字符进行百分比编码。您可能只需要使用 String.Replace 并从转义的 URI 中替换上面的字符。

关于c# - .NET UrlEncode 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30932903/

相关文章:

c# - 奇怪的异常场景 System.Net.Http MediaTypeHeaderValue 无法转换为 MediaTypeHeaderValue

asp.net - 在 LINQ 获取查询中添加虚拟列

c# - 在 ASP.NET 中将 HTML 内容写入 Word 文档时出现问题

jquery - asp.NET (4.0) 与 jqModal - 在 IE8 中,服务器端按钮提交存在问题

c# - 从 View 发送消息到 ViewModel : what is an appropriate token?

c# - 如何根据距离数据在面板上分配点?

c# - BlobContainerClient.CreateIfNotExistsAsync 方法的替代方法以避免 409 http 错误

asp.net - 我们可以使用 http.sys 在 linux 容器上运行应用程序吗?

c# - Mailkit 身份验证失败

c# - 将 float 格式化为小数点后两位