在静态方法中找不到request.querystring

本文关键字:request querystring 找不到 静态方法 | 更新日期: 2023-09-27 18:25:16

我有一个静态方法,我想在其中提取请求的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调用调用这个web方法。它在页面加载中工作良好,但在静态方法中不工作。我如何在静态方法中使用它。请协助。

在静态方法中找不到request.querystring

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

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

您必须在调用的同时传递查询字符串。这可以通过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();
int itemid =Convert.ToInt32(HttpContext.Current.Request.Form["itemid"]);

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

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

//在您的Webmethod 中调用此

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

简单

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

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

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

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

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

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

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