1

我有以下网址: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 项目,我没有设置任何特定的路由来处理处理程序,所以我不知道这是否会导致问题。

4

1 回答 1

1

尝试使用双引号:

context.Request.QueryString["w"]
于 2013-10-18T20:43:48.033 回答