简体   繁体   English

为什么Request.QueryString [“key”]停止工作?

[英]Why has Request.QueryString[“key”] stopped working?

I have a simple web form called default.aspx in the folder structure webroot/folder/ 我在文件夹结构webroot / folder /中有一个名为default.aspx的简单Web表单

When I navigate to http://myapp/folder/?key=value the page returns fine and when I call 当我导航到http://myapp/folder/?key=value ,页面返回正常,当我打电话时

<%= Request.QueryString[0] %>

I get http://myapp/folder/?key=value rendered on the page. 我在页面上显示http://myapp/folder/?key=value However if I call 但是,如果我打电话

<%= Request.QueryString["key"] %>

I get nothing, and when I call 我什么都没得到,当我打电话的时候

<%= Request.QueryString[1] %>

I get Index was out of range. Must be non-negative and less than the size of the collection. 我得到Index was out of range. Must be non-negative and less than the size of the collection. Index was out of range. Must be non-negative and less than the size of the collection.

This seems like a very trivial problem but I can't figure out what's going on?! 这似乎是一个非常微不足道的问题,但我无法弄清楚发生了什么?!

So it turns out that behind the scenes Sitecore turns the querystring into 事实证明,在幕后,Sitecore将查询字符串转换为

?page=the-requested-page.aspx?key=value ?页=的请求,page.aspx?key = value的

But the url in the browser looks as requested. 但浏览器中的URL看起来像请求的那样。 Obviously sticking a second ? 显然坚持一秒钟? in the actual url makes everything after the second ? 在实际的网址中,第二个之后的所有内容? disappear 消失

If you apply a breakpoint, and then hover over the QueryString in Visual Studio then you should be able to view all of the keys. 如果应用断点,然后将鼠标悬停在Visual Studio中的QueryString上,则应该能够查看所有键。 Alternatively you can foreach over the collection and print out the key names to see if it's slightly different to what you're expecting. 或者,您可以预览集合并打印出关键名称,看它是否与您期望的略有不同。

If you're doing something that isn't really supposed to be under Sitecore control (although I have never found it to make working with the QueryString collection impossible), try adding your /folder/ to the ignoreUrl setting. 如果你正在做一些不应该在Sitecore控件之下的东西(虽然我从来没有发现它使得无法使用QueryString集合),尝试将你的/ folder /添加到ignoreUrl设置。 Sitecore will get out of your hair, then ;-) 然后,Sitecore会脱离你的头发;-)

您的查询字符串应该在/default.aspx?key = value中,那么您将能够使用[“key”] param访问

you can use 您可以使用

Request.QueryString.Count();

and get the count of querystring modifiy code accordingly 并相应地获取查询字符串修改代码的计数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM