在 .NET 中创建 Uri 会自动对传递的字符串中的所有参数进行 url 解码

发布于 2024-12-03 03:47:48 字数 499 浏览 1 评论 0原文

假设我想从以下字符串创建一个 Uri 对象:

string url = @"http://someserver.com?param1=1&url=http%3a%2f%2fwww.otherserver.com";
Uri uri = new Uri(url, UriKind.Absolute);

预期结果是:

http://someserver.com?param1=1&url=http%3a%2f%2fwww.otherserver.com 

获得:

http://someserver.com/?param1=1&url=http://www.otherserver.com

在许多允许创建 Uri 的相关方法中注意到相同的行为:Uri.TryCreate、UriBuilder.Uri 等。

我如何获得 Uri保留初始编码参数?

Suppose I want to create an Uri object from the following string:

string url = @"http://someserver.com?param1=1&url=http%3a%2f%2fwww.otherserver.com";
Uri uri = new Uri(url, UriKind.Absolute);

Expected result would be:

http://someserver.com?param1=1&url=http%3a%2f%2fwww.otherserver.com 

Obtained:

http://someserver.com/?param1=1&url=http://www.otherserver.com

The same behavior is noticed in many related methods that allow Uri creation: Uri.TryCreate, UriBuilder.Uri, etc.

How would I get an Uri that preserve initial encoded parameter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

把回忆走一遍 2024-12-10 03:47:48

在 .NET4 中,您可以通过配置禁用某些方案的 Uri 压缩:

<configuration>
  <uri>
    <schemeSettings>
      <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
    </schemeSettings>
  </uri>
</configuration>

请注意,禁用默认行为会产生安全隐患。

In .NET4 you can disable Uri compaction for certain scheme via a configuration:

<configuration>
  <uri>
    <schemeSettings>
      <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
    </schemeSettings>
  </uri>
</configuration>

Note that there are security implications related to disabling of the default behaviour.

他不在意 2024-12-10 03:47:48

您是如何“获取”URL 的?如果我在 Visual Studio 中将鼠标悬停在它上面,它确实会显示解码后的 URL。

但每当我通过 AbsoluteUri 属性访问它时,它都会显示编码后的 URL。

How did you "obtain" the URL? If I hover my mouse over it in Visual Studio, it indeed shows the decoded URL.

But whenever I access it through the AbsoluteUri property, it shows the encoded URL.

故人的歌 2024-12-10 03:47:48

此行为已记录

作为某些方案构造函数中规范化的一部分,
转义表示被压缩。 URI 将针对的方案
紧凑转义序列包括以下内容:file、http、https、
net.pipe 和 net.tcp。对于所有其他方案,转义序列是
没有压实。例如:如果您将两个点“..”百分比编码为
"%2E%2E" 那么 URI 构造函数将压缩这个序列
计划。例如,以下代码示例显示了一个 URI
http 方案的构造函数。

因此,一种解决方法可能是暂时使用自定义方案(例如 leavemealone://)来构造 URL 对象(可能通过 UriBuilder?)。

This behavior is documented:

As part of canonicalization in the constructor for some schemes,
escaped representations are compacted. The schemes for which URI will
compact escaped sequences include the following: file, http, https,
net.pipe, and net.tcp. For all other schemes, escaped sequences are
not compacted. For example: if you percent encode the two dots ".." as
"%2E%2E" then the URI constructor will compact this sequence for some
schemes. For example, the following code sample shows a URI
constructor for the http scheme.

So one workaround might be temporarily using a custom scheme (e.g. leavemealone://) to construct the URL objects (possibly through UriBuilder?).

壹場煙雨 2024-12-10 03:47:48

就我而言,我通过返回 UriBuilder 类的 ToString() 方法而不是使用同一类的 Uri 属性来解决它。

In my case I solved it by returning ToString() method of UriBuilder class instead of using Uri property of the same class.

尐籹人 2024-12-10 03:47:48

我推荐使用 Flul 库。它允许流畅、正确地构建和分析 URI。看看他们的例子。 https://flurl.dev/docs/ Fluent-url/

它以 NuGet 形式提供包裹。你不需要 HTTP 的东西

    using Flurl;

    var url = "http://www.some-api.com"
    .AppendPathSegment("endpoint")
    .SetQueryParams(new {
        api_key = ConfigurationManager.AppSettings["SomeApiKey"],
        max_results = 20,
        q = "Don't worry, I'll get encoded!"
    })
    .SetFragment("after-hash");

I recommend using the library Flurl. It allows a fluent and correct build and analysis of URIs. See their examples. https://flurl.dev/docs/fluent-url/

It is available as NuGet package. You don't need the HTTP-Stuff

    using Flurl;

    var url = "http://www.some-api.com"
    .AppendPathSegment("endpoint")
    .SetQueryParams(new {
        api_key = ConfigurationManager.AppSettings["SomeApiKey"],
        max_results = 20,
        q = "Don't worry, I'll get encoded!"
    })
    .SetFragment("after-hash");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文