ASP.NET MVC SiteMap 与 Ninject 控制器工厂错误

发布于 2024-09-14 01:31:16 字数 3382 浏览 5 评论 0原文

我正在尝试将 ASP.NET MVC Codeplex Sitemap 项目与自定义动态节点提供程序一起使用。这是我的节点提供者代码。它使用一些存储库来访问数据库。实际上,我在此类中没有使用 DI,但应用程序继承了 NinjectHttpApplication 并且 Sitemap 在获取控制器时出现问题。

public class ContentPageDynamicNodeProvider : DynamicNodeProviderBase
{
    private IRepository _repository;

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        // Build value 
        var returnValue = new List<DynamicNode>();

        // Create a node for each content page
        _repository = new XmlDefaultRepository(ContentType.Page);
        foreach(var page in _repository.GetInstances())
        {
            DynamicNode node = new DynamicNode();
            node.Title = page.Title;
            node.ParentKey = "Default";
            node.RouteValues.Add("slug", page.Slug);
            returnValue.Add(node);
        }

        // Return 
        return returnValue;
    }
}

然后在 Site.Master 中,我尝试像这样渲染面包屑:

<%=Html.MvcSiteMap().SiteMapPath() %>

运行站点时启动的默认路由是 Page/Display/{slug} ,对于 slug 我使用“默认”,这里是 global.asax.cs 部分

        routes.MapRoute(
            "DefaultPage", RouteType.Regular,
            "",
            new { controller = "Page", action = "Display", slug = "Default" }, null
            );

        routes.MapRoute(
            "PageBySlug", RouteType.Regular,
            "{slug}",
            new { controller = "Page", action = "Display", slug = "Default" }, null
            );

        routes.MapRoute(
            "Default", RouteType.Regular,
            "{controller}/{action}/{slug}",
            new { controller = "Page", action = "Display", slug = "Default" }, null
            );

我遇到的问题是,当自定义站点节点提供程序就位时,我在运行时收到此错误:

值不能为 null 或为空。 参数名称:controllerName

这是堆栈跟踪(也许它会帮助某人识别正在发生的情况)。据我所知,它会转到 Ninject 作为控制器,但没有得到控制器。

[ArgumentException:值不能是 空或空。参数名称: 控制器名称]
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext,字符串控制器名称) +167 Ninject.Web.Mvc.NinjectControllerFactory.CreateController(RequestContext requestContext,字符串控制器名称) +151 MvcSiteMapProvider.AuthorizeAttributeAclModule.IsAccessibleToUser(IControllerTypeResolver 控制器类型解析器, DefaultSiteMapProvider 提供者, HttpContext 上下文、SiteMapNode 节点) +533 MvcSiteMapProvider.DefaultAclModule.IsAccessibleToUser(IControllerTypeResolver 控制器类型解析器, DefaultSiteMapProvider 提供者, HttpContext 上下文、SiteMapNode 节点) +149 MvcSiteMapProvider.DefaultSiteMapProvider.IsAccessibleToUser(HttpContext 上下文、SiteMapNode 节点)+24
System.Web.SiteMapNode.IsAccessibleToUser(HttpContext 上下文)+17
System.Web.SiteMapProvider.ReturnNodeIfAccessible(SiteMapNode 节点)+42
System.Web.StaticSiteMapProvider.FindSiteMapNode(字符串 原始网址)+176
MvcSiteMapProvider.DefaultSiteMapProvider.FindSiteMapNode(HttpContext 上下文)+282
System.Web.SiteMapProvider.get_CurrentNode() +56 MvcSiteMapProvider.Web.Html.SiteMapPathHelper.SiteMapPath(MvcSiteMapHtmlHelper 助手、字符串分隔符、字符串 分隔符CssClass,字符串 linkCSSClass,布尔值 当前节点作为链接,对象 html属性)+81
MvcSiteMapProvider.Web.Html.SiteMapPathHelper.SiteMapPath(MvcSiteMapHtmlHelper 助手)+30
ASP.views_site_master.__Render__control1(HtmlTextWriter __w,控制参数容器)+499

感谢任何帮助。

另请注意,如果没有自定义节点提供程序(仅 .sitemap 文件中的静态节点),一切正常。

I am trying to use the ASP.NET MVC Codeplex Sitemap project with Custom Dynamic Node provider. This is my node provider code. It uses some repository to access the DB. I actually don't use DI in this class but the application inherits NinjectHttpApplication and the Sitemap has problems acquiring controller.

public class ContentPageDynamicNodeProvider : DynamicNodeProviderBase
{
    private IRepository _repository;

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        // Build value 
        var returnValue = new List<DynamicNode>();

        // Create a node for each content page
        _repository = new XmlDefaultRepository(ContentType.Page);
        foreach(var page in _repository.GetInstances())
        {
            DynamicNode node = new DynamicNode();
            node.Title = page.Title;
            node.ParentKey = "Default";
            node.RouteValues.Add("slug", page.Slug);
            returnValue.Add(node);
        }

        // Return 
        return returnValue;
    }
}

Then in Site.Master I try to render the breadcrumbs like this:

<%=Html.MvcSiteMap().SiteMapPath() %>

My default route that starts when running the site is Page/Display/{slug} and for slug I use the "Default", here is the global.asax.cs part

        routes.MapRoute(
            "DefaultPage", RouteType.Regular,
            "",
            new { controller = "Page", action = "Display", slug = "Default" }, null
            );

        routes.MapRoute(
            "PageBySlug", RouteType.Regular,
            "{slug}",
            new { controller = "Page", action = "Display", slug = "Default" }, null
            );

        routes.MapRoute(
            "Default", RouteType.Regular,
            "{controller}/{action}/{slug}",
            new { controller = "Page", action = "Display", slug = "Default" }, null
            );

The problem I have is that when Custom Site Node Provider is in place I get this error at runtime:

Value cannot be null or empty.
Parameter name: controllerName

This is the stack trace (maybe it will help someone identify what is going on). As far as I can tell it goes to Ninject for controller but does not get one.

[ArgumentException: Value cannot be
null or empty. Parameter name:
controllerName]
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext
requestContext, String controllerName)
+167 Ninject.Web.Mvc.NinjectControllerFactory.CreateController(RequestContext
requestContext, String controllerName)
+151 MvcSiteMapProvider.AuthorizeAttributeAclModule.IsAccessibleToUser(IControllerTypeResolver
controllerTypeResolver,
DefaultSiteMapProvider provider,
HttpContext context, SiteMapNode node)
+533 MvcSiteMapProvider.DefaultAclModule.IsAccessibleToUser(IControllerTypeResolver
controllerTypeResolver,
DefaultSiteMapProvider provider,
HttpContext context, SiteMapNode node)
+149 MvcSiteMapProvider.DefaultSiteMapProvider.IsAccessibleToUser(HttpContext
context, SiteMapNode node) +24
System.Web.SiteMapNode.IsAccessibleToUser(HttpContext
context) +17
System.Web.SiteMapProvider.ReturnNodeIfAccessible(SiteMapNode
node) +42
System.Web.StaticSiteMapProvider.FindSiteMapNode(String
rawUrl) +176
MvcSiteMapProvider.DefaultSiteMapProvider.FindSiteMapNode(HttpContext
context) +282
System.Web.SiteMapProvider.get_CurrentNode()
+56 MvcSiteMapProvider.Web.Html.SiteMapPathHelper.SiteMapPath(MvcSiteMapHtmlHelper
helper, String separator, String
separatorCssClass, String
linkCssClass, Boolean
currentNodeAsLink, Object
htmlAttributes) +81
MvcSiteMapProvider.Web.Html.SiteMapPathHelper.SiteMapPath(MvcSiteMapHtmlHelper
helper) +30
ASP.views_site_master.__Render__control1(HtmlTextWriter
__w, Control parameterContainer) +499

Any help appreciated.

Also please note that without the custom node provider (just the static nodes in .sitemap file) everything works fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-残月青衣踏尘吟 2024-09-21 01:31:16

只是一个想法,但是您需要添加控制器和操作值吗?

node.RouteValues.Add("controller", "Page");
node.RouteValues.Add("action", "Display");

Just a thought, but do you need to add the controller and action values?

node.RouteValues.Add("controller", "Page");
node.RouteValues.Add("action", "Display");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文