【发布时间】:2011-04-13 17:34:03
【问题描述】:
我有网址:http://site.com/page.aspx?update
如何检查更新值是否存在?
HttpValueCollection 将其视为具有null 键的实体。我试过了:
var noKeyValues = Request.QueryString.GetValues(null);
if (noKeyValues != null && noKeyValues.Any(v=>v==update)) ...
但它让我皱眉头,因为 GetValues 的参数用 [NotNull] 修饰。
所以我最终做了:
var queryValuesWithNoKey =
Request.QueryString.AllKeys.Select((key, index) => new { key, value = Request.QueryString.GetValues(index) }).Where(
item => item.key == null).Select(item => item.value).SingleOrDefault();
if (queryValuesWithNoKey != null && queryValuesWithNoKey.Any(v => v.ToLower() == "update")) live = true;
不是最优雅的解决方法。有没有更好的方法从查询字符串中获取无键值?
【问题讨论】: