Cannot Get RazorView from Windsor Container using MVC 3 Beta
This is how my Application_Start looks:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
_container.Register(Component.For<IWindsorContainer>()
.Instance(_container),
Component.For<IView, IViewPageActivator>()
.ImplementedBy<RazorView>(),
Component.For<IFilterProvider>()
.ImplementedBy<WindsorFilterAttributeFilterProvider>(),
Component.For<IControllerFactory>()
.ImplementedBy<WindsorControllerFactory>(),
Component.For<ControllerContext>()
.ImplementedBy<ControllerContext>()
);
_container.Register(
AllTypes.Of<IController>()
.FromAssembly(Assembly.GetExecutingAssembly())
.Configure(c => c.LifeStyle.Transient)
);
Yet when trying to run the solution I get the following error:
Can't create component 'System.Web.Mvc.RazorView' as it has dependencies to be
satisfied.
System.Web.Mvc.RazorView is waiting for the following dependencies:
Keys (components with specific keys)
- viewPath which was not registered.
- layoutPath which was not registered.
- runViewStartPages which was not registered.
- viewStartFileExtensions which was n开发者_如何转开发ot registered.
How do I setup the container such that it can get the required information dynamically at run time? As I assume at the very least viewPath will change for each Controller.
I'm still playing around with MVC, but I think I can point you in the right direction.
When you registered the component for RazorView you want to use the DynamicParamters method - eg:
Component.For<IView>().ImplementedBy<RazorView>()
.DynamicParameters((kernel, dict) => {
dict["viewPath"] = "~";
dict["layoutPath"] = "~";
dict["runViewStartPages"] = true;
dict["viewStartFileExtensions"] = new List<string>() { "cshtml"};
})
I also didn't have Windsor cast RazorView as an IViewPageActivator since it does not implement that interface. If you read The post from Brad Wilson your implementation of IDependencyResolver should return null for the IViewPageActivator if you don't have one.
精彩评论