Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

request.querystring not found in static method

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.

like image 806
Rajaram Shelar Avatar asked Dec 01 '22 03:12

Rajaram Shelar


1 Answers

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.

like image 114
juhan_h Avatar answered Dec 05 '22 04:12

juhan_h