如何避免UriBuilder在查询字符串中使用未编码的+号创建Uri? [英] How to avoid that UriBuilder creates Uri with unencoded + sign in its query string?

查看:322
本文介绍了如何避免UriBuilder在查询字符串中使用未编码的+号创建Uri?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.Net中的UriBuilder和Uri类遇到一些麻烦.我想用UriBuilder构建我的Uri,然后使用生成的Uri.但是,我无法在查询字符串中正确编码加号吗?

I have some trouble with the UriBuilder and Uri classes in .Net. I want to build my Uri with a UriBuilder, and then use the resulting Uri. However, I cant get it to correctly encode the plus sign in its query string?

这是一个小的代码示例:

Here is a small code example:

var ub = new UriBuilder();
ub.Query = "t=a%2bc";
Console.WriteLine(ub.Uri.ToString());

此示例为我提供了http://localhost/?t=a+c,但是我希望加号像这样http://localhost/?t=a%2bc一样被编码为%2b,否则我将无法使用该网址.

This example gives me http://localhost/?t=a+c, but I would expect that the plus sign was encoded to %2b like this http://localhost/?t=a%2bc otherwise I cant use the url.

我当然可以构建一个字符串,但是如果可能的话,我宁愿使用强类型的Uri.

I could of course build a string instead, but I would prefer to use the strongly typed Uri if possible.

推荐答案

有趣的是,这似乎在.NET 4.5中已修复".

Interestingly, this seems to be "fixed" in .NET 4.5.

这是我在.NET 4.0 中进行测试的结果:(从立即窗口显示)

This is the result of my testing in .NET 4.0: (from the immediate window)

? ub.Uri.ToString()
"http://localhost/?t=a+c"

.NET 4.5 中的

But :

? ub.Uri.ToString()
"http://localhost/?t=a%2bc"

您要寻找的是什么.

您可以升级到4.5吗?这样可以解决您的问题.

Can you upgrade to 4.5? This would fix your problem.

如果您无法升级,请告诉我,我将尝试找到解决方法.

If you cannot upgrade, let me know and I'll attempt to find a work around.

这篇关于如何避免UriBuilder在查询字符串中使用未编码的+号创建Uri?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆