【问题标题】:request.querystring not found in static method在静态方法中找不到 request.querystring
【发布时间】:2013-07-29 05:41:06
【问题描述】:

我有一个静态方法,我想在其中提取请求的querystring 值。但是当我从webmethod 调用它时,它给了我null 的价值。下面是一些代码

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"
        }

我从 ajax 调用中调用了这个 webmethod。它在页面加载中工作正常,但在静态方法中却不行。我如何在静态方法中使用它。请帮忙。

【问题讨论】:

  • 这段代码的上下文是什么?不管是不是静态方法;重要的是:这是什么thread?您现在是否可能处于 ASP.NET 管道之外的某个回调线程或事件/计时器线程上?
  • 它是简单的静态方法。我从 webmethod 调用这个函数
  • 再一次,它是静态的这一事实是无关紧要的; 调用这个静态方法的代码是什么?这里重要的是调用上下文
  • 从Webmethod调用这个函数
  • 有没有机会显示更多的调用代码?例如,这是异步 [WebMethod] 对的 EndXXXX 方法吗?

标签: c# asp.net static-methods webmethod pagemethods


【解决方案1】:

您的静态方法中没有HttpContext.Current,因为您的静态方法没有当前上下文。

当您的静态方法在执行 Http 请求的线程上执行时,它应该可以工作。 要解决此限制,您应该将 HttpContext.Current.Request.QueryString 作为参数提供给您的静态函数表单 PageLoad 事件或您在请求生命周期中的任何位置。

【讨论】:

    【解决方案2】:

    您必须将查询字符串与调用一起传递。 这可以通过您的 ajax 调用来实现。

       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 () { }
                 });
    

    那么服务器端变量就可以正常访问了。

    string value = HttpContext.Current.Request.QueryString["itemId"].ToString();
    

    【讨论】:

      【解决方案3】:
      int itemid =Convert.ToInt32(HttpContext.Current.Request.Form["itemid"]);
      

      【讨论】:

      • 感谢您的回复,但仍然无法正常工作。它给出了空值。
      【解决方案4】:

      只需将查询字符串作为参数传入WebService的方法即可。

      【讨论】:

        【解决方案5】:
        //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;  
            }  
        

        //在你的 Web 方法中调用它

        NameValueCollection collection = GetQueryStringCollection(HttpContext.Current.Request.UrlReferrer.Query);  
                if (collection != null && collection.Count > 0)  
                {  
                    string id = HttpContext.Current.Server.UrlDecode (collection["id"]);  
                } 
        

        【讨论】:

          【解决方案6】:

          简单

              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";
              }
          

          【讨论】:

            【解决方案7】:

            如果您使用路由器,请尝试使用 RouteData

             string userIdQuery = string.Empty;               
             var userIdRouterValue = HttpContext.Current.Request.RequestContext.RouteData.Values["UserID"];
                        if (userIdRouterValue!=null)
                        {
                            userIdQuery = userIdRouterValue.ToString();
                        }
            

            【讨论】:

              【解决方案8】:

              第一步是您需要创建一个接受 2 个参数的 web 服务和 web 方法,即

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

              之后,您只需创建一个 Web 服务对象并调用 helloworld 方法,即

              public Ripple.WebAdmin webService = new Ripple.WebAdmin();
              webService.helloworld("username",password);
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2016-08-31
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-11-07
                • 2020-02-27
                • 1970-01-01
                • 2019-09-02
                相关资源
                最近更新 更多