MVC5 Autofac: The view found at was not created

2019-05-18 09:14发布

I created very simple MVC 5.0 application that I push to GitHub repository: https://github.com/marxin/mvc-sample. My motivation is to execute app on Linux with mono 3.2.3.

I would like to add Autofac NuGet package (more precisely 3.3.0), that works fine for me. Problem is that if I add Autofac.Mvc5 integration package, Razor stops working with following error:

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 

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

Could you please help me what's the necessary minimum that must be registered for Autofac to enable Razor view pages?

If I try aspx template, all works fine.

Thank you, Martin

2条回答
聊天终结者
2楼-- · 2019-05-18 10:08

OK, the problem was in web.config reference definition, please see following diff: https://github.com/marxin/mvc-sample/commit/3d5d7fef6f0284a96518852e0d892ca6205f5679

Btw. MVC5 is really not compatible with mono, but there are pull requests that will fix missing features.

Thanks

查看更多
forever°为你锁心
3楼-- · 2019-05-18 10:08

You need to register your IoC container for autofac. In your 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...
}
查看更多
登录 后发表回答