Skip navigation.

Don't Trust MSDN InstallerAll recent postsEuropean English

Wishful Thinking: Streamlined Handling of QueryString

I wish there was a way in ASP.NET to streamline handling of query strings. The amount of work that goes into parsing a query string and avoiding dereferencing nulls if it’s improperly formatted, etc, is ridiculous. You have to write this logic; it takes your focus away from the task the web form is supposed to perform and injects an unhealthy amount of mundane code. Yes, you can refactor it and move it into helper classes and such, but you still need to chase page fields and properties and initialize them with carefully extracted query string variables.

You probably chop up HttpRequest.QueryString to do one of the following:

  1. Extract query string variables and initialize page fields or properties.
  2. Set visibility of some page controls based on what variables appear in the query string.
  3. Construct a new URL using variables from the query string and then do a redirect/transfer.
  4. Etc.

I think the best candidate for automation of such tasks are custom attributes. I imagine you would decorate a field or property with a query string variable and have the page initialize it via reflection. You would also decorate a method to invoke or an event to fire if the query string was incorrect (didn’t contain what was expected).

Would it be nice if the Page class could take care of it? I’ve found a grand total of one such project at CodeProject, but it only initializes fields and properties. Who’s done this already?

Comments

Comment permalink 1 Ben Wong |
PageMethods is probably sort of what you're looking for.
Comment permalink 2 Ryan Farley |
PageMethods does look nice. I've always liked this approach http://www.codeproject.com/aspnet/WebParameter.asp (which is probably the one you're referring to on CodeProject). However it does only initialize fields/properties from the querystring. I do like how PageMethods handles building Urls and as parameters to methods.

-Ryan

Emails and Notifications

Would you like to be notified when somebody responds to this post?  Would you like to have these comments emailed to you?

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):