【发布时间】:2013-10-18 20:31:59
【问题描述】:
我有以下网址:http://localhost:60815/Image.ashx?w=202&h=0&f=somefile
通过以下方式访问其中一个参数时出现Index was out of range. Must be non-negative and less than the size of the collection. 错误:
Int32.TryParse(context.Request.QueryString['w'], out width);
但是,我可以使用索引访问值,即:
Int32.TryParse(context.Request.QueryString[0], out width);
当我查看查询字符串时(在调试即时窗口中),它确实知道键:
context.Request.QueryString
{w=202&h=0&f=somefile}
[System.Web.HttpValueCollection]: {w=202&h=0&f=somefile}
base {System.Collections.Specialized.NameObjectCollectionBase}: {w=202&h=0&f=somefile}
AllKeys: {string[3]} // [0]=w, [1]=h, [2]=f
有人可以解释为什么会这样吗?通过索引访问它们不是问题,但我想知道为什么通过密钥访问不起作用。
注意:我的项目是一个 MVC 项目,我没有设置任何特定的路由来处理处理程序,所以我不知道这是否会导致问题。
【问题讨论】:
-
您是否尝试过使用双引号的 context.Request.QueryString["w"]?
-
哦,哎呀。我今天一直在做 PHP,所以甚至没有注意到我在使用单引号。它将它转换为我假设的字符,这就是导致问题的原因 blush
-
我会把它作为答案发布。它现在对你有用吗?
-
是的,谢谢。我会接受的。谢谢爱尔兰人。
标签: asp.net routing query-string handler