2

这就是我的 Application_Start 的外观:

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)
    );

然而,当尝试运行解决方案时,我收到以下错误:

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 not registered. 

如何设置容器以便它可以在运行时动态获取所需信息?正如我假设的那样,至少每个控制器的 viewPath 都会发生变化。

4

1 回答 1

3

我仍在使用 MVC,但我认为我可以为您指明正确的方向。

当您为 RazorView 注册组件时,您希望使用 DynamicParamters 方法 - 例如:

Component.For<IView>().ImplementedBy<RazorView>()
    .DynamicParameters((kernel, dict) => {
        dict["viewPath"] = "~";
        dict["layoutPath"] = "~";
        dict["runViewStartPages"] = true;
        dict["viewStartFileExtensions"] = new List<string>() { "cshtml"};
    })

我也没有 Windsor 将 RazorView 转换为 IViewPageActivator,因为它没有实现该接口。如果您阅读Brad Wilson的帖子,您的 IDependencyResolver 实现应该为 IViewPageActivator 返回 null(如果您没有)。

于 2010-10-13T12:06:31.403 回答