【问题标题】:Detect Synchronous and Asynchronous Controller Actions in MVC 4在 MVC 4 中检测同步和异步控制器操作
【发布时间】:2015-08-18 19:01:08
【问题描述】:

我们最近遇到了 BaseController 中的 ExecuteCore() 不再被调用的问题。在 MVC 3 中工作但不在 MVC 4 中工作

所以,我将属性 protected override bool DisableAsyncSupport 添加到 BaseController 中,如下所示:

protected override bool DisableAsyncSupport
{
    get { return true; }
}

现在明显的问题是当我们得到一个异步操作时该怎么办? 如何检测控制器动作是同步还是异步?

我想我需要这样的东西:

protected override bool DisableAsyncSupport
{
    get
    {
        if(actionIsSynchronous)
            return true;
        else
            return false

    }
}

感谢您的帮助!

【问题讨论】:

  • 您是否尝试过使用 BeginExecuteCore 而不是 ExecuteCore?
  • 不,我没有。请解释。谢谢

标签: c# asp.net-mvc-3 asp.net-mvc-4 controller base-class


【解决方案1】:

您可以尝试使用 BeginExecuteCore

,而不是使用 ExecuteCore
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
    return base.BeginExecuteCore(callback, state);
}

另一种解决方案可能是覆盖 Initialize 方法。

protected override void Initialize(RequestContext requestContext)
{
      //Put your code here
}

【讨论】:

  • 如果我实现它,我认为我不再需要它? base.ExecuteCore();
  • 这应该替换 base.ExecuteCore();还有 DisableAsyncSupport
猜你喜欢
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2012-12-07
相关资源
最近更新 更多