httpcontext.current.request default return value if not set

Josh picture Josh · Jul 3, 2012 · Viewed 11.7k times · Source

I've spent a lot of time googling this and testing in my program, but I can't get any output. I'm coding in C# codebehind aspx. I'm trying to query the url of

anUnknownWebsite.aspx?SerialNumber=CND0340482

And getting the serial number with the httpcontext.current.request["SerialNumber"] method.

One half of my program works fine, where if the serial number is set in the URL, everything works fine, but I want to have a boolean statement like the following:

    if(HttpContext.Current.Request["SerialNumber"] == null)
    {
    Do this
    }else
    {
    Do something else
    }

I just want to know if the method will have a default value or a null value to get a boolean from because the if statement above doesn't work. Open to try any new ideas or theories on this.

Thanks guys

Answer

McGarnagle picture McGarnagle · Jul 3, 2012

It should be null (not an empty string / default value), but try this...

if(HttpContext.Current.Request.QueryString["SerialNumber"] == null) {
    // ...
}

If you know SerialNumber is in the query string, you can use Request.QueryString, otherwise you'll be also be checking the form variables, cookies, and server variables.