本文介绍了MVC 4 beta 中未触发基类中的 ExecuteCore()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的控制器类:

I have a base controller class:

我所有的其他控制器都像这样继承了这个 BaseClass

And all my other controller inherits this BaseClass like this

所有这些在 MVC3 中都很好用(今天再次测试,它确实有效)但似乎 BaseController 中的 ExecuteCore 在 MVC 4 beta 中不再被触发.

All this works great in MVC3 (test again today, it really works) but it seems that the ExecuteCore in BaseController is not fired any more in MVC 4 beta.

有什么想法吗?或者引擎盖下发生了什么巨大的变化?非常感谢.

Any idea? Or Anything huge has changed under the hood? Thanks very much.

public class BaseController : Controller
{
    private string _myData;

    public string MyData
    {
        get
        {
            return _myData;
        }
    }

    protected override void ExecuteCore()
    {
        _myData = "I am doing something";

        base.ExecuteCore();
    }
}


public class HomeController : BaseController
{
    public ActionResult Index()
    {
        ViewBag.MyData = MyData;
        // Doing something with value in BaseClass

        return View();
    }
}

推荐答案

我能够重现您的问题.ExecuteCore 的用法好像变了.但我没有找到任何关于它的信息.我猜这与现在 Controller 实现 IAsyncController 而不是 AsyncController 的事实有关.

I was able to reproduce your problem. It seems that the usage of ExecuteCore is changed. But I haven't find any information about it. My guess it's related to the fact that now the Controller implements IAsyncController not the AsyncController.

但是我找到了一种解决方法来获得 MVC4 的旧行为:

However I've found a workaround to get the old behavior with MVC4:

将此添加到 BaseContoller :

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

来自 DisableAsyncSupport(我强调添加):

From The MSDN page for DisableAsyncSupport (emphasis add by me):

这个标志是为了向后兼容.ASP.NET MVC 4. 允许控制器支持异步模式.这意味着在派生类上不会调用 ExecuteCore.如果派生类仍然需要调用 ExecuteCore,它们可以覆盖此标志并设置为 true.

这篇关于MVC 4 beta 中未触发基类中的 ExecuteCore()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 12:01