4

我有静态方法,我想在其中提取querystring请求的值。但是null当我从webmethod. 下面是一些代码

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。它在页面加载中工作正常,但在静态方法中却不行。我如何在静态方法中使用它。请协助。

4

8 回答 8

10

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

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

于 2013-07-29T12:32:14.903 回答
6

您必须将查询字符串与调用一起传递。这可以通过您的 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();
于 2016-02-09T10:44:01.430 回答
3
int itemid =Convert.ToInt32(HttpContext.Current.Request.Form["itemid"]);
于 2013-07-29T14:32:56.917 回答
2

您只需要将查询字符串作为参数传递到 WebService 的方法中。

于 2013-07-30T09:33:57.653 回答
0

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

 string userIdQuery = string.Empty;               
 var userIdRouterValue = HttpContext.Current.Request.RequestContext.RouteData.Values["UserID"];
            if (userIdRouterValue!=null)
            {
                userIdQuery = userIdRouterValue.ToString();
            }
于 2019-01-31T04:26:48.727 回答
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;  
    }  

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

NameValueCollection collection = GetQueryStringCollection(HttpContext.Current.Request.UrlReferrer.Query);  
        if (collection != null && collection.Count > 0)  
        {  
            string id = HttpContext.Current.Server.UrlDecode (collection["id"]);  
        } 
于 2015-07-18T09:14:28.823 回答
0

简单的

    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";
    }
于 2017-06-07T16:32:23.437 回答
-1

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

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

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

public Ripple.WebAdmin webService = new Ripple.WebAdmin();
webService.helloworld("username",password);
于 2013-07-30T09:44:12.310 回答