HttpContext Items Not Available to Web Method Called by AJAX
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)]
精彩评论