在 C# 中,如何构造和解析嵌套查询字符串?
我正在编写一个 API,希望人们能够提供 Google Charts API 调用作为参数。在参数包含完全独立的 API 调用的情况下,解构这个有问题的 API 调用的正确方法是什么?
例如:
?method=createimage&chart1=https://chart.googleapis.com/chart?chs=250x100&chd=t:60,40&cht=p3&chl=Hello|World
在上面的示例中,我想将其视为 (2) 个查询字符串键:method 和 chart1。我是否可以将上面的示例解析为 2 个查询字符串键,使 Google Charts API 调用保持完整,而不是将其分解?我可以将调用封装为 JSON 或类似的内容吗?
非常感谢!干杯
I'm writing an API and would like people to be able to supply a Google Charts API call as an argument. What's the proper way to deconstruct this problematic API call, where an argument contains an entirely separate API call?
For instance:
?method=createimage&chart1=https://chart.googleapis.com/chart?chs=250x100&chd=t:60,40&cht=p3&chl=Hello|World
In the example above, I'd like to think of it as (2) query string keys: method and chart1. Is it possible for me to parse the above example as 2 query string keys, leaving the Google Charts API call intact, rather than breaking it down? Could I enclose the call as JSON or something along those lines?
Thanks much! Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是正确的方法(使用 ParseQueryString 方法):
如果您愿意构造此查询字符串:
哦,顺便说一下,您在问题中显示的是一个无效的查询字符串,我所显示的第二个代码片段的输出证实了这一点。您应该对
chart1
参数进行 URL 编码。查询字符串中包含多个?
字符绝对违反所有标准。正确的查询字符串如下所示:
Here's the proper way (using the ParseQueryString method):
and if you wanted to construct this query string:
Oh and by the way, what you have shown in your question is an invalid query string which is confirmed by the output of the second code snippet I have shown. You should URL encode your
chart1
parameter. It's absolutely against all standards to have more than one?
character in a query string.Here's how the correct query string would look like:
您应该对查询字符串中的 URL 进行 URL 编码,因为它包含保留字符。另外,十六进制编码也可以正常工作。
完成此操作后,您可以分别处理这两个值,并且解析很简单。
You should URL encode the URL in your query string, as it contains reserved characters. Alternatively, hex encoding works just fine too.
Once you have done so, you can treat the two values separately and parsing is simple.