简体   繁体   English

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

[英]request.querystring not found in static method

I have static method in which I want to extract querystring value of the request.我有一个静态方法,我想在其中提取请求的querystring值。 But it gives me null value when i am calling it from webmethod .但是当我从webmethod调用它时它给了我null值。 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.我正在从ajax 调用中调用这个 webmethod。它在页面加载中工作正常,但在静态方法中无效。 How do I use this in static method.我如何在静态方法中使用它。 Please assist.请协助。

You do not have HttpContext.Current in your static method, because your static method has no current context. 您的静态方法中没有HttpContext.Current ,因为静态方法没有当前上下文。

It should work, when your static method is executed on a thread that is executing a Http request. 当您的静态方法在执行Http请求的线程上执行时,它应该工作。 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. 要解决此限制,您应该将HttpContext.Current.Request.QueryString作为参数提供给静态函数表单PageLoad事件或您处于请求生命周期中的任何位置。

You have to pass the query string along with the call. 您必须将查询字符串与调用一起传递。 this can be achieved from your ajax call. 这可以通过你的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 () { }
             });

Then server side variables can be accessed as normal. 然后可以正常访问服务器端变量。

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

//Call this in your Webmethod //在Webmethod中调用它

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

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

If you using router, try using RouteData 如果您使用路由器,请尝试使用RouteData

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

The first step is that you need to create a webservice and web method that accepts 2 parameters ie 第一步是您需要创建一个接受2个参数的Web服务和Web方法,即

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

After that you simply create an object of a webservice and call the helloworld method ie 之后,您只需创建一个Web服务对象并调用helloworld方法即

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM