有没有办法将Ajax Post发送到不调用静态函数的ASPX页面?

时间:2011-10-01 22:05:09

标签: asp.net ajax

这个问题说明了一切。我可以毫不费力地将Ajax调用到我的ASPX页面 - 只要我使用静态方法。但是,我想在会话中存储结果,因此需要“实时”函数调用。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

发布到Generic Http处理程序(.ashx)并让它继承自IRequireSession接口。然后你可以保存到会话。

答案 1 :(得分:0)

我已经对这个问题进行了相当多的研究,虽然@ latr0dectus的回答可能有效,但我希望从页面内部实现相当多的回调。也许我需要做更多的研究,但我没有找到在Generic Http处理程序中调用多个方法的方法,也没有找到传递复杂参数的方法。所以,这对我来说效果不佳。

更简单的解决方案就是使用WebService并使用[WebMethod(EnableSession = true)]属性。

您可以从Ajax访问的会话感知Web服务的完整示例是:

using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.SessionState;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class SCMasterService : System.Web.Services.WebService {

    public SCMasterService () {
    }

    [WebMethod(EnableSession = true)]
    public string GetSummaryList(string ID)
    {
        return Session["SomeVal"];
    }
}

要从Javascript / JQuery / Ajax“Post”调用它,您需要两个关键行:

[System.Web.Script.Services.ScriptService]

使此Web服务可用于Ajax和

[WebMethod(EnableSession = true)]

在被调用的方法中启用会话管理。

相关问题