空的QueryString参数 [英] Empty QueryString parameter

查看:113
本文介绍了空的QueryString参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用asp.net在以下URL的查询字符串中检查 foo 参数的正确方法是什么?这有可能吗?

What is the proper way to check for the foo parameter in the following url's querystring using asp.net? Is this even possible?

http://example.com?bar=3&foo

我尝试检查 Request ["foo""以及 Request.QueryString [" foo]] ,但得到 null 两者皆可.我还尝试用 QueryString 集合中的值填充 List ,但是正如我在下面提到的那样,它不包含该值.

I have tried checking Request["foo"] as well as Request.QueryString["foo"] and I get null for both. I have also tried populating a List with the values from the QueryString collection but as I mention below, it does not include the value.

我了解没有任何价值,但是 Request ["foo"] 不应返回空字符串,而不是 null 吗?有没有办法找出查询字符串键是否存在,即使它没有值?

I understand that there is no value, but shouldn't Request["foo"] return an empty string rather than null? Is there a way to find out if a querystring key exists even if it has no value?

我发现此处那个 Request.QueryString.AllKeys 包含用于空白查询字符串参数的 null .

I found here that Request.QueryString.AllKeys includes null for blank querystring parameters.

如下所述,由 James

As stated below by James and Dreas a Regex to parse the raw url might be the best (and possibly only) approach.

Regex.IsMatch(Request.RawUrl, "[?&]thumb([&=]|$)")

推荐答案

您可以使用 null 作为 NameValueCollection 的键,它将为您提供一个逗号分隔的符号没有值的参数名称列表.

You can use null as the key for the NameValueCollection and it will give you a comma-delimited list of parameter names that don't have values.

对于 http://example.com?bar=3&foo ,您将使用 Request.QueryString [null] ,它将检索 foo .

For http://example.com?bar=3&foo you would use Request.QueryString[null] and it would retrieve foo.

如果您有多个不带值的参数名,它将为您提供一个以逗号分隔的值.

If you have more than one parameter name without a value, it will give you a value that is comma-delimited.

对于 http://example.com?bar=3&foo&test ,您将获得 foo,test 作为返回值.

For http://example.com?bar=3&foo&test you would get foo,test as a value back.

更新:

您实际上可以使用 Request.QueryString.GetValues(null)来获取没有值的参数名称.

You can actually use Request.QueryString.GetValues(null) to get the parameter names that don't have values.

这篇关于空的QueryString参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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