Click here to Skip to main content
15,884,472 members
Articles / Web Development / ASP.NET
Article

Request Query String / Form Parametrs

Rate me:
Please Sign up or sign in to vote.
2.77/5 (17 votes)
8 Mar 20061 min read 107.5K   21   4
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.

C#
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:

C#
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


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionmmm... are you sure? Pin
Cradle778-Mar-06 8:02
Cradle778-Mar-06 8:02 
Hi Jakob,
i'm not so sure you're completely right.

1) the Item[String] accessor of HttpRequest class does almost the same work you wrote: (looking at reflector)
public string this[string key]<br />
{<br />
      get<br />
      {<br />
string text1 = this.QueryString[key];<br />
            if (text1 != null)<br />
            {<br />
                  return text1;<br />
            }<br />
            text1 = this.Form[key];<br />
            if (text1 != null)<br />
            {<br />
                  return text1;<br />
            }<br />
            // more code goes here....<br />

with the difference that it checks Cookies and ServerVariables collection too, so, where should the performance optimization be?

2) maybe i'm missing the point, but your alghorithm doesn't seem to work properly: if you are searching for a QueryString key and you got some Form entry too (that makes Request.Form.Count being major than zero), it returns string.Empty even if QueryString contains that value.

BTW, returning string.Empty instead of null is a good way to avoiding some NullReferenceException bug.

Bye!

Marco De Sanctis
AnswerRe: mmm... are you sure? Pin
Leon Kovach8-Mar-06 22:14
Leon Kovach8-Mar-06 22:14 
GeneralRe: mmm... are you sure? Pin
Cradle778-Mar-06 22:48
Cradle778-Mar-06 22:48 
GeneralGood Insight Pin
Prasad Khandekar8-Mar-06 5:53
professionalPrasad Khandekar8-Mar-06 5:53 

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

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