Why is Request.QueryString readonly?
Asked Answered
S

2

6

I thought you couldn't change the QueryString on the server without a redirect.

But this code works* for me:

Request.QueryString edit

I'm so amazed.

So here are my questions regarding this:

  1. Why is Request.QueryString readonly?
  2. Why does this code/hack work*?
  3. How safe is it, if you change to readonly as soon as you are done editing, both regarding bad errors or unexpected behaviour, and regarding maintaining and understanding the code?
  4. Where in the event cycle would it make most sense to do this crazy edit if you are only using PageLoad and OnPageRender?

*More details:

I have a page with items that are grouped into tabs. Each tab is an asp:LinkButton

I want to be able to link directly to a specific tab. I do that with a QueryString parameter 'tab=tabName'. It works. But when I then click a new tab, the querystring is still in the Url, and thus the tab specified in the Querystring gets activated and not the one I clicked.

By using Request.QueryString edit this does not happen. Then my solution 'works'.

Thanks in advance.

Sib answered 11/2, 2011 at 11:58 Comment(2)
Can you post the markup for your page, I find that when I'm butting heads with the framework that I'm generally doing something wrong or stupid or both.Candelaria
My code is a bit involved so it's probably not something to post unless I trim it down a bit for this question. Thanks for the tip.Sib
W
10

Well the QueryString property is readonly because it cannot be changed on a single request. Obviously the browser sends only one request with only one string so only one collection is created. The hack uses reflection (i.e. manipulates the code and the memory) to change stuff that you cannot change normally. This hack breaks the encapsulation and the design of the QueryString property. You should not use it. It makes no sense from design standpoint. Your query DOES NOT change so why change the object that represents it? Only the browser can send new query string so you are basically lying to your own code about what the browser sent.

If you want the tabs to use the URL just use Hyperlinks instead of LinkButton.

Whereas answered 11/2, 2011 at 12:5 Comment(2)
Thanks for the reply. How do I get rid of the QueryString parameter on subsequent postbacks?Sib
Response.Redirect. However I doubt you want the tabs to do a post. It may be the case but usually this is not the best way to do it.Whereas
A
2

From what I remember reading, this is a security standard that all browsers adhere to. It's main purpose is to stop phishing attacks, where someone could have the website www.MyLameWarcraftPhishingSite.com" and when someone hits the page, rewrite the url to look like www.blizzard.com. The only way to get to that url is to actually redirect to it.

mmm, last post was in Feb 11 - hope its ok to post in this.

Alyce answered 7/6, 2011 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.