避免创建多个控制器实例

时间:2017-12-01 08:04:43

标签: c# ajax asp.net-mvc asp.net-mvc-5 asp.net-ajax

我试图用ajax获取一些数据,这些变量在控制器构造函数中初始化,然后根据代理的值进行更改。

但问题是当我用ajax获取数据时它总是返回默认值(0),委托的更改在执行ajax调用时不会影响变量。我想它每次执行ajax时都会创建一个新的控制器实例。

    private int waiting;
    private int all;

    public HomeController()
    {
        all = 0;
        waiting = 0;
        Connect();
    }

    public async Task Connect()
    {
        pbx.WaitingCallsChanged += Changed;
        pbx.PhoneCallsChanged += PhoneChanged;
    }

    public void PhoneChanged(object sender, PhoneChangedEventArgs e)
    {
        all = e.Phone.ActiveCalls.Count;
    }

    private void Changed(object sender, WaitingCallsChangedEventArgs e)
    {
        waiting = e.Queue.WaitingCalls.Count;
    }

    public ActionResult GetWaiting()
    {
        return Json(new { queue1 = waiting, all = all }, JsonRequestBehavior.AllowGet);
    }

0 个答案:

没有答案