THX*_*138 3 asp.net-mvc query-string
我有网址:http : //site.com/page.aspx?update
如何检查该更新值是否存在?
HttpValueCollection将其视为具有null键的实体。我试过了:
var noKeyValues = Request.QueryString.GetValues(null);
if (noKeyValues != null && noKeyValues.Any(v=>v==update)) ...
Run Code Online (Sandbox Code Playgroud)
但它让我皱眉,因为 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;
Run Code Online (Sandbox Code Playgroud)
不是最优雅的解决方法。有没有更好的方法从查询字符串中获取无键值?
您可以使用
Request.QueryString[null]
Run Code Online (Sandbox Code Playgroud)
检索没有值的逗号分隔的键列表。例如,如果您的网址是:
http://mysite/?first&second
那么上面的将返回
first,second
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您可以执行以下操作:
if(Request.QueryString[null] == "update")
{
// it's an update
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1343 次 |
| 最近记录: |