代码之家  ›  专栏  ›  技术社区  ›  Bullines

HttpContext项对AJAX调用的Web方法不可用

  •  2
  • Bullines  · 技术社区  · 15 年前

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

    我可以通过任何一个页面的方法访问这个页面。例如:

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

    这个很好用。

    但是,当通过AJAX调用页面的一个web方法时,失败:

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

    异步调用的HttpContext与页面的HttpContext不同吗?

    2 回复  |  直到 15 年前
        1
  •  4
  •   John Saunders    15 年前

    HttpContext.Items 只保存项目 在一次请求中 Items 财产。

        2
  •  -1
  •   Ray    15 年前

    可能需要启用会话状态才能使其正常工作:

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