request.querystring not found in static method

Rajaram Shelar picture Rajaram Shelar · Jul 29, 2013 · Viewed 20k times · Source

I have static method in which I want to extract querystring value of the request. But it gives me null value when i am calling it from webmethod. Below is the some code

public static int GetLatestAssetId()
    {
        int itemid=0;
        if (HttpContext.Current.Request.QueryString["itemId"] != null)
        itemid = Convert.ToInt32(HttpContext.Current.Request.QueryString["itemId"]);
        return itemid;
    }

[WebMethod]

        public static string GetContactData()
        {

            GetLatestAssetId();
            return "Success"
        }

I am calling this webmethod from the ajax call.It works fine in page load but not in static method. How do I use this in static method. Please assist.

Answer

juhan_h picture juhan_h · Jul 29, 2013

You do not have HttpContext.Current in your static method, because your static method has no current context.

It should work, when your static method is executed on a thread that is executing a Http request. To get around this limitation you should supply HttpContext.Current.Request.QueryString as a parameter to your static function form PageLoad event or where ever you are in your request life-cycle.