I'm trying to inject a dependency into the shared layout view page to avoid having to do it in every view that uses the layout.
I've followed the guidance in the wiki for injecting dependencies into views, but the property is always null.
Can Autofac inject properties into a custom view page that is a layout file?
Here's my setup. CustomViewPage
namespace MyApp
{
using System.Web.Mvc;
public abstract class CustomViewPage : WebViewPage
{
public IHelper Helper { get; set; }
}
}
~/Views/Shared/_Layout.cshtml
@inherits MyApp.CustomViewPage
<!DOCTYPE html>
<html>
...
@if(this.Helper.HasFoo()){@Html.ActionLink("Bar")}
Global Registration...
builder.RegisterType<Helper>().AsImplementedInterfaces();
builder.RegisterModelBinderProvider();
builder.RegisterFilterProvider();
builder.RegisterModule(new AutofacWebTypesModule());
builder.RegisterSource(new ViewRegistrationSource());
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
The "child" views that use the layout do NOT derive from the CustomViewPage.