开发者

HttpContext Items Not Available to Web Method Called by AJAX

开发者 https://www.devze.com 2022-12-19 15:43 出处:网络
I have an item that I store in the HttpContext: HttpContext.Current.Items[\"myItem\"] = \"123\"; I can access this no problem from any of a page\'s methods.For example:

I have an item that I store in the HttpContext:

HttpContext.Current.Items["myItem"] = "123";

I can access this no problem from any of a page's methods. For example:

protected override void OnLoad(EventArgs e)
{
    string l_myItemVal = HttpContext.Current.Items["myItem"] as string; // "123"
}

This works fine.

However, when calling one of the page's web methods via AJAX, this fails:

[System开发者_开发技巧.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static string MyWebMethod()
{
    string l_myItemVal = HttpContext.Current.Items["myItem"] as string; // NULL
}

Is the HttpContext of an asynchronous call different from the HttpContext for the page?


HttpContext.Items only holds items during a single request. Your AJAX request is a second request, and has it's own Items property.


Maybe you need to enable session state to make this work:

[System.Web.Services.WebMethod(true)]
0

精彩评论

暂无评论...
验证码 换一张
取 消