【问题标题】:Uri.EscapeDataString behaves different in NUnit test vs. asp.net webservice?Uri.EscapeDataString 在 NUnit 测试与 asp.net 网络服务中的行为不同?
【发布时间】:2017-04-21 18:31:00
【问题描述】:

我正在使用Uri.EncodeDataString 在 URL 上发送查询字符串。原始字符串为:Photo($select=Name,Id) 在我在 VS2015 调试器中运行的 asp.net Web 服务中,Uri.EncodeDataString 将返回:Photo(%24select%3DName%2CId) 但是,在 VS2015 的 Resharper 测试运行器下运行的 NUnit 测试中,它返回:Photo%28%24select%3DName%2CId%29。注意括号编码的不同。

为什么括号在单元测试中被编码,而在 Web 服务中却没有?括号不应该在 Web 服务中编码吗?我在 .Net 4.6 下运行,所以我不应该受到 StackOverflow 上其他地方讨论的 RFC 3986 问题的影响(hereherehere 和网络上的许多其他地方)。

【问题讨论】:

    标签: c# .net encoding percent-encoding


    【解决方案1】:

    最后,我确实受到了 RFC 3986 问题的影响。

    事实证明,不同之处在于 .Net 4.5 的“怪癖”模式。在 asp.net 应用程序中运行时,除非明确设置,否则 .Net 将尝试保持应用程序与以前版本的兼容性。

    我进入了框架代码,在 UriParser 中发现了一个古怪的设置。谷歌搜索将我带到this article,了解 web.config 中的 <httpruntime> 元素。接近尾声时,它说:

    "如果没有<httpRuntime targetFramework> 属性存在于 Web.config,我们假设应用程序需要 4.0 怪癖 行为。”

    在我将targetFramework="4.5" 添加到我的 web.config 后,Uri.EscapeDataString 的行为符合我的预期。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多