request.querystring not found in static method
Asked Answered
M

8

5

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.

Milliken answered 29/7, 2013 at 12:23 Comment(7)
what is the context of this code? it doesn't matter whether or not it is a static method; what matters mainly is: what thread is this? is it possible you are now on a callback thread or an event / timer thread for something that is outside of the ASP.NET pipeline?Bernice
it is in simple static method. I am calling this function from webmethodMilliken
again, the fact that it is static is irrelevant and unrelated; what code is calling this static method? it is the call context that matters hereBernice
Calling this function from WebmethodMilliken
any chance you can show more of the calling code? for example, is this the EndXXXX method of an async [WebMethod] pair?Bernice
that is intriguing; can you confirm: is it HttpContext.Current that is returning null ? It strikes me as odd that this wouldn't work; but: have you tried using a regular web-method rather than a page-method? (i.e. non-static etc)Bernice
No I haven't tried this for webmethod. HttpContext.Current gives me count of querystring as 0Milliken
H
10

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.

Hindmost answered 29/7, 2013 at 12:32 Comment(0)
G
6

You have to pass the query string along with the call. this can be achieved from your ajax call.

   var qString = "?" + window.location.href.split("?")[1];
      $.ajax({ 
              url: "<aspx pagename>/<ajax method>" + qString,
              data: {},
              dataType: "json",
              contentType: "application/json; charset=utf-8",
              type: "POST",
              success: function(){},
              error: function () { },
              completed: function () { }
             });

Then server side variables can be accessed as normal.

string value = HttpContext.Current.Request.QueryString["itemId"].ToString();
Gesner answered 9/2, 2016 at 10:44 Comment(0)
B
3
int itemid =Convert.ToInt32(HttpContext.Current.Request.Form["itemid"]);
Brittle answered 29/7, 2013 at 14:32 Comment(1)
Thanks for your response but still it is not working. It is giving null value.Milliken
F
2

You only need to pass the query string as a parameter into the method of WebService.

Frig answered 30/7, 2013 at 9:33 Comment(0)
C
0
//Get Querystring name value collection  
    public static NameValueCollection GetQueryStringCollection(string url)  
    {  
        string keyValue = string.Empty;  
        NameValueCollection collection = new NameValueCollection();  
        string[] querystrings = url.Split('&');  
        if (querystrings != null && querystrings.Count() > 0)  
        {  
            for (int i = 0; i < querystrings.Count(); i++)  
            {  
                string[] pair = querystrings[i].Split('=');  
                collection.Add(pair[0].Trim('?'), pair[1]);  
            }  
        }  
        return collection;  
    }  

//Call this in your Webmethod

NameValueCollection collection = GetQueryStringCollection(HttpContext.Current.Request.UrlReferrer.Query);  
        if (collection != null && collection.Count > 0)  
        {  
            string id = HttpContext.Current.Server.UrlDecode (collection["id"]);  
        } 
Chignon answered 18/7, 2015 at 9:14 Comment(0)
G
0

Simple

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

    [WebMethod]
    public static string GetData()
    {
        int x = GetQueryStringData();
        if (x != 0)
            return "Success";
        else
            return "not success";
    }
Glee answered 7/6, 2017 at 16:32 Comment(0)
M
0

If you using router, try using RouteData

 string userIdQuery = string.Empty;               
 var userIdRouterValue = HttpContext.Current.Request.RequestContext.RouteData.Values["UserID"];
            if (userIdRouterValue!=null)
            {
                userIdQuery = userIdRouterValue.ToString();
            }
Ming answered 31/1, 2019 at 4:26 Comment(0)
F
-1

The first step is that you need to create a webservice and web method that accepts 2 parameters i.e.

[WebMethod]
public void helloworld(string name,string password
{
}

After that you simply create an object of a webservice and call the helloworld method i.e.

public Ripple.WebAdmin webService = new Ripple.WebAdmin();
webService.helloworld("username",password);
Frig answered 30/7, 2013 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.