Request.QueryString[] 与 Request.Query.Get() 与 HttpUtility.ParseQueryString()
我搜索了SO并发现了类似的问题,但没有一个比较这三个问题。这让我很惊讶,所以如果有人知道,请指出我。
有多种不同的方法来解析请求的查询字符串......“正确”的方法(IMO)应该处理空值/缺失值,但也可以适当地解码参数值。以下哪一项是同时实现这两点的最佳方法?
方法1
string suffix = Request.QueryString.Get("suffix") ?? "DefaultSuffix";
方法2
string suffix = Request.QueryString["suffix"] ?? "DefaultSuffix";
方法3
NameValueCollection params = HttpUtility.ParseQueryString(Request.RawUrl);
string suffix = params.Get("suffix") ?? "DefaultSuffix";
方法4
NameValueCollection params = HttpUtility.ParseQueryString(Request.RawUrl);
string suffix = params["suffix"] ?? "DefaultSuffix";
问题:
如果未指定后缀,
Request.QueryString["suffix"]
是否会返回 null? (我知道这是一个令人尴尬的基本问题)HttpUtility.ParseQueryString()
是否比直接访问Request.QueryString
提供任何额外的功能?MSDN 文档列出了此警告:
ParseQueryString 方法使用可能包含用户输入的查询字符串,这是潜在的安全威胁。默认情况下,ASP.NET 网页验证用户输入不包含脚本或 HTML 元素。有关详细信息,请参阅脚本漏洞利用概述。
但我不清楚这是否意味着应该使用ParseQueryString()
来处理该问题,或者因此而面临安全缺陷......它是哪一个?ParseQueryString()
默认使用 UTF8 编码...所有浏览器默认都以 UTF8 编码查询字符串吗?- 如果指定了多个值,
ParseQueryString()
将以逗号分隔值...Request.QueryString()
是否也这样做,或者如果它会发生什么不是吗? 哪些方法可以正确地将“%2b”解码为“+”?
再次展示我的 Windows 开发根源...如果我不那么想知道这些事情的话,我会成为一个更快的开发人员...:P
I searched SO and found similar questions, but none compared all three. That surprised me, so if someone knows of one, please point me to it.
There are a number of different ways to parse the query string of a request... the "correct" way (IMO) should handle null/missing values, but also decode parameter values as appropriate. Which of the following would be the best way to do both?
Method 1
string suffix = Request.QueryString.Get("suffix") ?? "DefaultSuffix";
Method2
string suffix = Request.QueryString["suffix"] ?? "DefaultSuffix";
Method 3
NameValueCollection params = HttpUtility.ParseQueryString(Request.RawUrl);
string suffix = params.Get("suffix") ?? "DefaultSuffix";
Method 4
NameValueCollection params = HttpUtility.ParseQueryString(Request.RawUrl);
string suffix = params["suffix"] ?? "DefaultSuffix";
Questions:
Would
Request.QueryString["suffix"]
return a null if no suffix was specified?
(Embarrassingly basic question, I know)Does
HttpUtility.ParseQueryString()
provide any extra functionality over accessingRequest.QueryString
directly?The MSDN documentation lists this warning:
The ParseQueryString method uses query strings that might contain user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.
But it's not clear to me if that means
ParseQueryString()
should be used to handle that, or is exposed to security flaws because of it... Which is it?ParseQueryString()
uses UTF8 encoding by default... do all browsers encode the query string in UTF8 by default?ParseQueryString()
will comma-separate values if more than one is specified... doesRequest.QueryString()
do that as well, or what happens if it doesn't?Which of those methods would correctly decode "%2b" to be a "+"?
Showing my Windows development roots again... and I would be a much faster developer if I didn't wonder about these things so much... : P
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
方法#1 和方法#2 确实是一样的。 (我认为
.Get()
方法是为了语言兼容性而提供的。)ParseQueryString
返回的内容与Request.Querystring
功能等效。 。当您有原始 URL 并且没有其他方法可以从中解析查询字符串参数时,通常会使用它。Request.Querystring
会为您完成此操作,因此在本例中不需要它。你不能省略
“后缀”
。您必须传递一个字符串或一个索引号。如果完全省略[]
,您将获得整个NameValueCollection
。如果您的意思是如果"suffix"
不是 QueryString 值之一,那么是的;如果您调用Request.QueryString["suffix"]
,您将得到null
。没有。 从中解析查询字符串参数。
ParseQueryString
确实不处理它...也不能直接从Request.QueryString
中提取值。对于 ASP.NET,您通常将表单值作为控件的值来处理,而这就是 ASP.NET 通常为您“处理”这些事情的地方。 换句话说:永远不要相信用户输入。无论哪个框架为您做任何事情。我不知道(我想没有)。但是,我认为您正在阅读的内容告诉您
ParseQueryString
返回 UTF-8 编码文本 - 无论它传入时是否如此编码。再次:
ParseQueryString
返回的内容与从Request.QueryString
获得的内容基本相同。事实上,我认为ParseQueryString
在内部使用来提供Request.QueryString
。他们会产生等价物;他们都会正确解码提交的值。如果您有 URL:
http://site.com/page.aspx?id=%20Hello
然后调用Request.QueryString["id"]
返回值将为“Hello”
,因为它会自动解码。Methods #1 and #2 are the same thing, really. (I think the
.Get()
method is provided for language compatibility.)ParseQueryString
returns you something that is the functional equivalent ofRequest.Querystring
. You would usually use it when you have a raw URL and no other way to parse the query string parameters from it.Request.Querystring
does that for you, so in this case, it's not needed.You can't leave off
"suffix"
. You either have to pass a string or an index number. If you leave off the[]
entirely, you get the wholeNameValueCollection
. If you mean what if"suffix"
was not one of the QueryString values then yes; you would getnull
if you calledRequest.QueryString["suffix"]
.No. The most likely time you would use it is if you had an external URL and wanted to parse the query string parameters from it.
ParseQueryString
does not handle it... neither does pulling the values straight fromRequest.QueryString
. For ASP.NET, you usually handle form values as the values of controls, and that is where ASP.NET usually 'handles' these things for you. In other words: DON'T TRUST USER INPUT Ever. No matter what framework is doing what ever for you.I have no clue (I think no). However, I think what you are reading is telling you that
ParseQueryString
is returning UTF-8 encoded text - regardless if it was so encoded when it came in.Again:
ParseQueryString
returns basically the same thing you get fromRequest.QueryString
. In fact, I thinkParseQueryString
is used internally to provideRequest.QueryString
.They would produce the equivalent; they will all properly decode the values submitted. If you have URL:
http://site.com/page.aspx?id=%20Hello
then callRequest.QueryString["id"]
the return value will be" Hello"
, because it automatically decodes.示例 1:
直接回答您的问题:
Example 1:
Stright to your questions: