我有静态方法,我想在其中提取请求的 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 方法。它在页面加载中运行良好,但在静态方法中运行不佳。我如何在静态方法中使用它。请协助。
最佳答案
您的静态方法中没有 HttpContext.Current
,因为您的静态方法没有当前上下文。
当您的静态方法在执行 Http 请求的线程上执行时,它应该可以工作。
要解决此限制,您应该提供 HttpContext.Current.Request.QueryString
作为静态函数表单的参数 PageLoad
事件或您在请求生命周期中的任何位置.
关于c# - 在静态方法中找不到 request.querystring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17923933/