是否有一个 URL 生成器也支持请求参数串联?
我想要实现如下目标:
UrlBuilder ub = new UrlBuilder("http://www.google.com/search");
ub.Parameters.Add("q","request");
ub.Parameters.Add("sourceid","ie8");
string uri = ub.ToString(); //http://www.google.com/search?q=request&sourceid=ie8
.NET 中有什么东西吗,或者我必须创建自己的东西?
I want to achieve something like the following:
UrlBuilder ub = new UrlBuilder("http://www.google.com/search");
ub.Parameters.Add("q","request");
ub.Parameters.Add("sourceid","ie8");
string uri = ub.ToString(); //http://www.google.com/search?q=request&sourceid=ie8
Is there anything in .NET, or I will have to create my own?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
据我所知,没有任何东西存在。 这是一些简单的东西,可以满足您的需求。 用法是:
==
代码是:
Nothing exists that I know of. Here's something simple which does what you want. Usage would be:
==
Code is:
UriBuilder 类有帮助吗?
它没有任何方法来添加查询字符串参数。 查看查询属性来设置值。
编辑:请参阅 UriTemplate 类。
Does the UriBuilder class help?
It doesn't have any method to add querystring parameters. Look at Query property to set values.
EDIT: See UriTemplate class.
我自己开发的,更适合我的需求,感谢您的代码:
I developed my own, that's more suitable for my needs, thanks for your code:
我建议您查看关于 CodeProject 的这篇文章。
作者扩展了 System.UriBuilder 类并添加了一个QueryString 属性的行为方式与 HttpRequest 大致相同.QueryString 属性。
使用此类,您的示例将变为:
它没有像 流畅的界面 ="https://stackoverflow.com/questions/908507/is-there-a-url-builder-that-supports-request-paramater-concatenation-as-well/908552#908552">乔希的解决方案但是可以很容易地扩展到包括一个。
I would recommend you take a look at this article on CodeProject.
The author has extended the System.UriBuilder class and added a QueryString property that behaves in much the same way as the HttpRequest.QueryString property.
Using this class your example would become:
It doesn't have a fluent interface like Josh's solution but could be easily extended to include one.
使用 Flurl [披露:我是作者],您的示例将如下所示:
基本 URL 生成器可通过 NuGet 获取:
PM> Install-Package Flurl
还有一个新的配套库,它使用 flutter 扩展了 Flurl ,可测试 HTTP:
PM>; 安装包 Flul.Http
With Flurl [disclosure: I'm the author], your example would look like this:
The basic URL builder is available via NuGet:
PM> Install-Package Flurl
There's also a new companion lib that extends Flurl with fluent, testable HTTP:
PM> Install-Package Flurl.Http