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

我有一种静态方法,我想提取该值
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.

加载页面时,它的工作很大,但不是静态方法。 如何以静态方法使用它。 请帮忙。
已邀请:

快网

赞同来自:

你没有
HttpContext.Current

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

当在执行请求的流中执行静态方法时,它应该工作。 Http.
要解决这个限制,你必须提供
HttpContext.Current.Request.QueryString

作为静态功能的参数 form
PageLoad

event 或无论您在您的请求的生命周期中。

冰洋

赞同来自:

您必须与挑战一起传输查询字符串。
这可以使用您的呼叫实现。 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//;


</ajax></aspx>

石油百科

赞同来自:

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 它是你的网页方法


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方法 2 参数,即。


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


之后,您只需创建Web服务对象并调用方法 helloworld, 它是。


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

要回复问题请先登录注册