Asp.net - 空查询参数 [英] Asp.net - Empty QueryString Parameter

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

问题描述

问题

什么是检查使用asp.net在以下网址的查询字符串参数的正确方法是什么?这甚至可能?

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

我已经试过检查请求[富] 以及的Request.QueryString [富] 我也得到两种。我也曾尝试填充列表查询字符串收藏价值,但正如我下面提,它不包括值。

问题

据我所知,是没有价值的,但不应该请求[富] 返回一个空字符串,而不是?有没有办法找出如果查询字符串项存在,即使它有没有价值?

注释

我发现<一个href=\"http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=377511\">here该 Request.QueryString.AllKeys 包括空白查询字符串参数。

正如詹姆斯下文所述及的 Dreas 一个正则表达式解析URL原料可能是最好的(也可能是唯一)的办法。

  Regex.IsMatch(Request.RawUrl[?&安培]拇指([安培; =] | $))


解决方案

您可以使用作为键的NameValueCollection ,它会给你一个逗号分隔的参数名列表不具有价值。

有关 http://example.com?bar=3&foo 你可以使用的Request.QueryString [空] ,它将检索

如果你没有一个值有多个参数的名称,它会给你一个值是逗号分隔。

有关 http://example.com?bar=3&foo&test 您将获得富,测试作为一种价值回来了。

更新:

您可以实际使用 Request.QueryString.GetValues​​(空)来得到那些没有价值的参数名。

The Problem

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

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.

The Question

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?

Notes

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

[edit]

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([&=]|$)")

解决方案

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.

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.

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

Update:

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

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

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