URL 查询字符串 - 查找、替换、添加、更新值? [英] URL Querystring - Find, replace, add, update values?

查看:73
本文介绍了URL 查询字符串 - 查找、替换、添加、更新值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们从另一家公司的项目中继承了一些 C# 代码,该公司执行 URL 重定向,可根据需要修改现有查询字符串、更改项目值、添加新参数等.然而,问题是代码充其量是有问题的,最终会复制查询字符串中的项目而不是正确更新它们.该代码在第一次通过时有效,但在额外调用时,重复问题变得明显.

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.

例如:MyPage.aspx?startdate=08/22/09&startdate=09/22/09

Ex: 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.

是否有用于处理查询字符串的 C# 类或一组函数,允许一种简单的方法来访问和更新/添加解决这些问题的参数,而不是现在似乎与代码一起使用的盲目添加方法?这需要能够处理多个参数,这些参数可能一直存在也可能不存在,并在后续调用中添加和更新.

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.

如果可能的话,我们会更早地使用现有逻辑而不是重新创建一些东西,以便以半标准的方式快速解决这个问题,以实现未来的可维护性和重用.

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.

推荐答案

是的,我建议使用 HttpUtility.ParseQueryString()

Yes I would suggest converting the querystring to a collection by using 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.

这篇关于URL 查询字符串 - 查找、替换、添加、更新值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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