1

在我的应用程序中,我有一个项目登记册。要访问这些项目是 url:/project/{url-project}

the value of `{url-project}` is defined in the registration of the project!

我使用这个项目来生成路径,但由于“注册项目”是动态的,因此必须手动实现它,如本文所示。

所以我做了:

public class ProjectDetailsDynamicNodeProvider : DynamicNodeProviderBase
{
    [Inject]
    public IProjectRepository ProjectRepository { get; set; }

    #region Overrides of DynamicNodeProviderBase

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        // Build value 
        var returnValue = new List<DynamicNode>();
        var listDB = (from p in ProjectRepository.Query()
                      select new { p.Name, p.ID });

        // Create a node for each album 
        foreach (var project in listDB.ToList())
        {
            var node = new DynamicNode { Title = project.Name };
            node.RouteValues.Add("id", project.ID);

            returnValue.Add(node);
        }

        // Return 
        return returnValue;
    }

    #endregion
}

在我的 Mvc.sitemap

<mvcSiteMapNode title="Projetos" controller="Project" action="Index">
  <mvcSiteMapNode title="Novo Projeto" controller="Project" action="Create" />
  <mvcSiteMapNode title="Projeto" action="About" dynamicNodeProvider="BindSolution.Infra.ProjectDetailsDynamicNodeProvider, BindSolution" />
</mvcSiteMapNode>

问题

我的问题是ProjectRepository始终为空!

kernel.Bind<IProjectRepository>().To<ProjectRepository>().InRequestScope();

但在我的设置中,Ninject 是正确的!该存储库在控制器中使用,并且运行良好

尝试

我试图通过构造函数注入这个依赖,但结果是一样的!

public class ProjectDetailsDynamicNodeProvider : DynamicNodeProviderBase
{
    public IProjectRepository ProjectRepository { get; set; }

    public ProjectDetailsDynamicNodeProvider(IProjectRepository projectRepository)
    {
        ProjectRepository = projectRepository
    }

    #region Overrides of DynamicNodeProviderBase

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        // Build value 
        var returnValue = new List<DynamicNode>();
        var listDB = (from p in ProjectRepository.Query()
                      select new { p.Name, p.ID });

        // Create a node for each album 
        foreach (var project in listDB.ToList())
        {
            var node = new DynamicNode { Title = project.Name };
            node.RouteValues.Add("id", project.ID);

            returnValue.Add(node);
        }

        // Return 
        return returnValue;
    }

    #endregion
}
4

1 回答 1

1

我相信这个问题和你的类似: MVC3, Ninject, MvcSiteMapProvider - How to injection dependency to overridden method

这意味着基本上你没有一个好的解决方案。

于 2012-04-18T15:00:58.393 回答