c# - ASP.NET MVC3 升级 - "Attempt by method [whatever] to access method System.Web.Mvc.Controller.View(...) failed"

标签 c# asp.net-mvc asp.net-mvc-3

我有一个在 MVC2 中运行良好的单元测试。该测试简单地定义 Controller 上的操作、必要的 stub ,并测试 View 的名称。但是,升级到 MVC3 后,当我调用该方法时,出现上述错误。站点 MVC3 升级工作正常;由于升级,我只是让这些单元测试失败了。谢谢。

这是我的操作:

public partial class GadgetController
{
    [SetterProperty]
    public IATCGadgetProxy ATCGadgetService { get; set; }

    public ActionResult LoadForums(bool popularOnly, bool myThreads, int itemCount)
    {
        var model = ATCGadgetService.LoadForums(popularOnly, myThreads, itemCount);

        return View("AskTheCommunity-Forums", model);
    }
}

这是测试。从操作返回 View 时失败。

[TestMethod]
public void Test_Forums_Action_Type()
{
    GadgetController controller = new GadgetController();
    controller.ATCGadgetService = new ATCGadgetServiceStub();
    ViewResult result = controller.LoadForums(false, false, 10) as ViewResult;

    Assert.IsNotNull(result);
    Assert.AreEqual("AskTheCommunity-Forums", result.ViewName);
}

最佳答案

我知道这是一个旧线程,但我在 MVC 5.2.3 中遇到了同样的错误...但使用了 Xunit。最后,这并不重要,因为解决问题的方法是一样的。

首先,您必须确保 MVC 已添加到您的测试项目中。我通过 NuGet 添加了它:

Install-Package Microsoft.AspNet.Mvc -Version 5.2.3

或者您可以将版本更改为您正在使用的任何 MVC 版本。

然后,我仍然有错误。我刚刚发现 App.config 页面缺少信息。一旦我确定我有这些行,一切正常:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

    <!-- Other config here -->

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

关于c# - ASP.NET MVC3 升级 - "Attempt by method [whatever] to access method System.Web.Mvc.Controller.View(...) failed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6698518/

相关文章:

asp.net-mvc-3 - 在所有 View 中共享布局内的下拉列表的方法是什么?

c# - 如何检查当前用户在 ASP.NET MVC 5 中的角色

c# - 如何在 C# 驱动程序中设置 MongoDB Change Stream 'OperationType'?

c# - IdentityUser 如何验证旧密码?

c# - 如何将对象作为可绑定(bind)属性传递给 Xamarin.Forms 自定义控件

c# - ASP.NET MVC POST 中的模型绑定(bind) IEnumerable?

asp.net-mvc-3 - 为什么 @Html.Label() 删除一些字符

asp.net-mvc - MVC 模型需要 true

c# - 了解 MVC 5 用户声明表

.net - 更改、覆盖、替换、asp.net MVC 3 默认 DataAnnotations 所需值和无效值的验证消息