【问题标题】:MVC5 Autofac: The view found at was not createdMVC5 Autofac:未创建找到的视图
【发布时间】:2014-01-31 19:21:02
【问题描述】:

我创建了非常简单的 MVC 5.0 应用程序,并将其推送到 GitHub 存储库:https://github.com/marxin/mvc-sample。我的动机是使用 mono 3.2.3 在 Linux 上执行应用程序。

我想添加 Autofac NuGet 包(更准确地说是 3.3.0),这对我来说很好用。问题是如果我添加 Autofac.Mvc5 集成包,Razor 会停止工作并出现以下错误:

System.InvalidOperationException
The view found at '~/Views/Home/Index.cshtml' was not created.

Description: HTTP 500.Error processing request.

Details: Non-web exception. Exception origin (name of application or object): System.Web.Mvc.
Exception stack trace:
at System.Web.Mvc.BuildManagerCompiledView.Render (System.Web.Mvc.ViewContext viewContext, System.IO.TextWriter writer) [0x00000] in <filename unknown>:0 at System.Web.Mvc.ViewResultBase.ExecuteResult (System.Web.Mvc.ControllerContext context) [0x00000] in <filename unknown>:0 at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult (System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ActionResult actionResult) [0x00000] in <filename unknown>:0 at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive (IList`1 filters, Int32 filterIndex, System.Web.Mvc.ResultExecutingContext preContext, System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ActionResult actionResult) [0x00000] in <filename unknown>:0 at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive (IList`1 filters, Int32 filterIndex, System.Web.Mvc.ResultExecutingContext preContext, System.Web.Mvc.ControllerContext controllerContext, System.Web.Mvc.ActionResult actionResult) [0x00000] in <filename unknown>:0 

差异:https://github.com/marxin/mvc-sample/commit/ca980adcf1d67e2e74729b27b11c26065dd2567e

您能否帮我注册 Autofac 以启用 Razor 视图页面的最低要求是多少?

如果我尝试使用 aspx 模板,一切正常。

谢谢你,马丁

【问题讨论】:

  • 当心,mono 还与 MVC5 不兼容(但我在这里实现了缺失的部分:github.com/mono/mono/pull/862,因此您可能希望帮助他将补丁拆分为多个拉取请求,以便于由单人队)

标签: c# razor mono autofac asp.net-mvc-5


【解决方案1】:

好的,问题出在 web.config 参考定义中,请参阅以下差异: https://github.com/marxin/mvc-sample/commit/3d5d7fef6f0284a96518852e0d892ca6205f5679

顺便说一句。 MVC5 确实与单声道不兼容,但有一些拉取请求会修复缺失的功能。

谢谢

【讨论】:

    【解决方案2】:

    您需要为 autofac 注册您的 IoC 容器。在您的 global.asax 中:

    protected void Application_Start()
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(typeof(MvcApplication).Assembly);
        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    
        // Other MVC setup...
    }
    

    【讨论】:

    • 我添加了您建议的代码:github.com/marxin/mvc-sample/blob/…,但它没有帮助。我阅读了可以在 Autofac wiki 上找到的 MvcIntegration wiki 页面,但它只涉及 Razor 的注入问题。我只需要使用 Razor。
    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2013-12-29
    • 1970-01-01
    • 2020-06-06
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多