C# URL 查询字符串 - 查找、替换、添加、更新值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1163956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
URL Querystring - Find, replace, add, update values?
提问by schooner
We inherited some C# code as part of a project from another company which does URL redirects that modifies the existing query string, changing values of items, adding new params, etc as needed. The issue however is that the code is buggy at best, and ends up duplicating items in the query string instead of updating them properly. The code works on the first pass but on additional calls the duplication issues become apparent.
我们从另一家公司的项目中继承了一些 C# 代码,该公司执行 URL 重定向,可根据需要修改现有查询字符串、更改项目值、添加新参数等。然而,问题是代码充其量是有问题的,最终会复制查询字符串中的项目而不是正确更新它们。该代码在第一次通过时有效,但在额外调用时,重复问题变得明显。
Ex: MyPage.aspx?startdate=08/22/09&startdate=09/22/09
例如:MyPage.aspx?startdate=08/22/09&startdate=09/22/09
Instead of duplicating the item it needs to be either updated with the new value if it already exists, or added if not there already.
如果项目已经存在,则需要使用新值更新,如果不存在则添加,而不是复制项目。
Is there a C# class or set of functions for handling query strings, allowing a simple means to access and update/add parameters that gets around these issues instead of the blind add approach that seems to be in use now with the code? This needs to be able to handle multiple parameters that may or may not exists at all times and be added and updated on subsequent calls.
是否有用于处理查询字符串的 C# 类或一组函数,允许一种简单的方法来访问和更新/添加解决这些问题的参数,而不是现在似乎与代码一起使用的盲目添加方法?这需要能够处理多个参数,这些参数可能一直存在也可能不存在,并在后续调用中添加和更新。
We would sooner use existing logic than recreate something if possible so as to get this resolved quickly in a semi standard way for future maintainability and reuse.
如果可能的话,我们会更早地使用现有逻辑而不是重新创建一些东西,以便以半标准的方式快速解决这个问题,以实现未来的可维护性和重用。
采纳答案by Rob Levine
Yes I would suggest converting the querystring to a collection by using HttpUtility.ParseQueryString()
是的,我建议使用将查询字符串转换为集合 HttpUtility.ParseQueryString()
You can then find/add/update/replace values directly in the collection, before re-creating the querystring from this collection.
然后,您可以直接在集合中查找/添加/更新/替换值,然后从此集合重新创建查询字符串。
This should make it easier to spot duplicates.
这应该可以更容易地发现重复项。
回答by Traveling Tech Guy
回答by b0x0rz
this seems a basic design problem.
这似乎是一个基本的设计问题。
instead of updating the current query string, what SHOULD be done is simply adding all the parameters to the base at every time.
应该做的不是更新当前的查询字符串,而是每次都将所有参数添加到基数中。
sure, you CAN update it, but (pseudocode)
当然,你可以更新它,但是(伪代码)
if querystring exists
then update query string
else
add query string
will get crazy when you start using more than 1 variable.
当您开始使用 1 个以上的变量时会变得疯狂。
redesign would be best, effort allowing.
重新设计将是最好的,努力允许。
回答by marc_s
The WCF REST Starter Kitavailable on ASP.NET also include a new "HttpQueryString" helper class that will most likely be included in the .NET 4.0 time frame into the base class library.
ASP.NET 上可用的WCF REST Starter Kit还包括一个新的“HttpQueryString”帮助程序类,该类很可能会在 .NET 4.0 时间框架中包含在基类库中。
See an excellent screencast on how to use this utility class here:
在此处查看有关如何使用此实用程序类的出色截屏视频:
Marc
马克