MVC5 Autofac:找不到的视图

时间:2014-01-31 19:21:02

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

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

我想添加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模板,一切正常。

谢谢你,马丁

2 个答案:

答案 0 :(得分:3)

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

顺便说一下。 MVC5实际上与单声道不兼容,但有拉动请求可以修复缺失的功能。

由于

答案 1 :(得分:1)

您需要注册您的IoC容器以进行autofac。在 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...
}