Click here to Skip to main content
Licence 
First Posted 8 Mar 2006
Views 74,565
Bookmarked 20 times

Request Query String / Form Parametrs

By | 8 Mar 2006 | Article
Request Query String / Form Parametrs Reducing Server Load

Introduction

Most of beginners developers that program in ASP or ASP.NET use Request object.
Usually they use it in the form of Request["Parametr_Name"] , while not taking into account the fact
that this way of using the object causes a great load on the server performing this task.
Meaning that the server load into memory the whole Request object collection and has to decide weather to activate
the Request.Form or Request.QueryString methods.

When the site serves a great number of users this can cause a great load on the server.

In this article I'll present a simple way to ease up the server load.

Using the code

In the code below I first check the Request.Form.Count if it's greater then zero,
then method="post" was used, and I read the given ParamName , ELSE
I check if Request.QueryString.Count is greater the zero and only then I decide if to read the parameter.
IF the result is null I turn it into String.Empty in This way I don't have to check if the value is null and only
then get it's ToString() method to convert it into string and then check it's value.

public string RequestParam(string ParamName)
{
    string Result = String.Empty;
    
    if (Context.Request.Form.Count != 0)
    {
        Result = Convert.ToString(Context.Request.Form[ParamName]);
    }
    else if (Context.Request.QueryString.Count != 0)
    {
        Result = Convert.ToString(Context.Request.QueryString[ParamName]);
    }

    return (Result==null) ? String.Empty : Result.Trim();
}

So when I use the above code I can do the following:

if (RequestParam("ZipCode").Length < 5)
{
    ErrorMesage = "Zip code must contain 5 numeric digits.");
}
In the "Old Way" you could not write if (Request["ZipCode"].ToString().Length <5) because if Request["ZipCode"] == null
it does not have a ToString() method.

Using the code above enables me to use the Request Parameters as a string and saves me writing code to determine if it's null.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Leon Kovach

Web Developer

Israel Israel

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionmmm... are you sure? PinmemberCradle778:02 8 Mar '06  
AnswerRe: mmm... are you sure? PinmemberJakob Solomon22:14 8 Mar '06  
GeneralRe: mmm... are you sure? PinmemberCradle7722:48 8 Mar '06  
GeneralGood Insight PinmemberPrasad Khandekar5:53 8 Mar '06  
Hello,
 
Thank's a lot for posting this nice tip and bringing to attention the typically ignored coding practice.
 
Regards,
 
Prasad P. Khandekar
Knowledge exists, man only discovers it.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 8 Mar 2006
Article Copyright 2006 by Leon Kovach
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid