数据库中的动态站点地图不显示节点 [英] Dynamic sitemap from database doesn't display the nodes

查看:61
本文介绍了数据库中的动态站点地图不显示节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了 https://github.com/maartenba/MvcSiteMapProvider/wiki/Defining-sitemap-nodes-using-IDynamicNodeProvider

编辑:这是我的课程

public class MyDynamicNodeProvider
: DynamicNodeProviderBase
{

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)

    { 
        webdata storeDB = new webdata();

        var returnValue = new List<DynamicNode>();


        foreach (var article in storeDB.SiteContents) 
        {


            DynamicNode enode = new DynamicNode();
            enode.Title = article.ArticleTitle;
            enode.ParentKey = "ArticleID"; 
            enode.Url = "ArticleDetails/" + article.ArticleID + "/" + article.ArticleAlias;
            //Specify Controller and Action name
            enode.Controller = "SiteContents";
            enode.Action = "ArticleDetails";
            enode.RouteValues.Add("id", article.ArticleID);
            returnValue.Add(enode);

            yield return enode;
        }


    }
}

修改:这是我的站点地图文件

Edit:this is my sitemap file

 <mvcSiteMapNode title="Home" controller="Home" action="Index">
 <mvcSiteMapNode title="About Us" controller="Menu" action="AboutUs">
 <mvcSiteMapNode title="Profile" controller="Menu"   action="Profile"/>
 <mvcSiteMapNode title="History" controller="Menu" action="History"/>
 </mvcSiteMapNode>
 <mvcSiteMapNode title="Article" controller="SiteContents"  action="ArticleDetails" key="ArticleID"> 
 <mvcSiteMapNode title="Details"  dynamicNodeProvider="Myproject.Models.MyDynamicNodeProvider, Myproject"  />
 </mvcSiteMapNode>

编辑:我拥有的第二个控制器(SiteContentsController)

Edit: The second controller which I have (SiteContentsController )

  public ActionResult ArticleDetails(int? id, string slug)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        SiteContents siteContents = db.SiteContents.Find(id);
        if (siteContents == null)
        {
            return HttpNotFound();
        }
        if (string.IsNullOrWhiteSpace(slug))
        {

            var alias = db.SiteContents.First(p => p.ArticleID == id).ArticleAlias;


            return RedirectToAction("ArticleDetails", new { id = id, slug = alias });
        }

        return View(siteContents);
      }

我想要的网址(可以正常工作,但不会带来站点地图是 http://localhost:xxxx/ArticleDetails/1/Quality_Policy

The url I would like to have (which work but it doesn't bring the sitemap is http://localhost:xxxx/ArticleDetails/1/Quality_Policy

然后我在布局页面上调用站点地图

and I call the sitemap at my layoutpage

@Html.MvcSiteMap().SiteMapPath()

修改:我的route.config

Edit: My route.config

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(name: "Articles", url: "ArticleDetails/{id}/{slug}", defaults: new { controller = "SiteContents", action = "ArticleDetails", id = UrlParameter.Optional, slug = UrlParameter.Optional });

        routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional});


    }

我也有一些可以正常工作的静态节点.问题是,在动态页面中使用不会返回任何内容,并且我也没有收到任何错误消息谢谢

I also have some static nodes which work ok. The problem is that with in dynamic pages returns nothing and I dont get any error message thank you

推荐答案

之所以不起作用,是因为您没有考虑所有路由值,即您有一个名为 slug的路由值,您需要配置该节点以使其匹配.

The reason why it is not working is that you have not accounted for all of the route values, namely you have a route value named slug which you need to configure the node to match.

如果您希望节点匹配而不考虑 slug 的值(即使其为空),则应使用 PreservedRouteParameters 进行匹配.否则,应将其添加到 RouteValues 中,并且该节点将仅匹配为其配置的一个值(如果需要,可以添加其他节点以匹配其他值).我在这里显示 PreservedRouteParameters 方法.

If you want the node to match regardless of the value for slug (even if it is blank), you should use PreservedRouteParameters to match it. Otherwise, you should add it to RouteValues and the node will only match the one value you configure for it (you can add additional nodes to match other values if needed). I am showing the PreservedRouteParameters approach here.

此外,通过在动态节点上配置 Url 属性,您已经有效地禁用了MVC支持.如果需要使用非MVC页面或外部URL,则此属性很有用,但不建议将其用于MVC.

In addition, you have effectively disabled MVC support by configuring the Url property on your dynamic node. This property is useful if you need to use a non-MVC page or an external URL, but is not recommended for MVC.

MvcSiteMapProvider 直接取决于MVC路由配置.在这里,您可以配置URL以使其看起来像您期望的样子.为了使预期的URL( http://localhost:xxxx/ArticleDetails/1/Quality_Policy )起作用,您需要一条相应的路由来匹配此模式,如下所示.

MvcSiteMapProvider depends directly on the MVC routing configuration. This is where you configure your URLs to look how you want them to look. For your expected URL (http://localhost:xxxx/ArticleDetails/1/Quality_Policy) to work, you need a corresponding route to match this pattern, as below.

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // Route to match the URL /ArticleDetails/1/Quality_Policy
        routes.MapRoute(
            name: "ArticleDetails",
            url: "ArticleDetails/{id}/{slug}",
            defaults: new { controller = "SiteContents", action = "ArticleDetails", slug = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

您遇到的另一个问题是要将动态节点附加到的节点.当前,配置节点的方式转到 ArticleDetails 操作.我无法告诉您您要在这里做什么.通常,您将显示所有文章页面的列表(索引),然后当用户单击文章时,将显示它.这是一个例子.

One other issue you have is the node you are attaching the dynamic nodes to. The way you have your node configured currently goes to the ArticleDetails action. I can't tell what you are trying to do here. Normally, you would show a list (index) of all of the article pages and then when the user clicks an article, you would show it. Here is an example.

// NOTE: Normally, you would put all of your Article stuff
// into an ArticleController
public class SiteContentsController
{
    // NOTE: Normally, this would be named ArticleController.Index()
    public ActionResult ArticleIndex()
    {
        // NOTE: You may want to use a view model here
        // rather than using the SiteContents directly.
        var siteContents = db.SiteContents.ToList();
        return View(siteContents);
    }

    // NOTE: Normally, this would be named ArticleController.Details()
    public ActionResult ArticleDetails(int? id, string slug)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        SiteContents siteContents = db.SiteContents.Find(id);
        if (siteContents == null)
        {
            return HttpNotFound();
        }
        if (string.IsNullOrWhiteSpace(slug))
        {

            var alias = db.SiteContents.First(p => p.ArticleID == id).ArticleAlias;


            return RedirectToAction("ArticleDetails", new { id = id, slug = alias });
        }

        return View(siteContents);
    }
}

您的 Mvc.sitemap 文件看起来更像这样(文章位于主页下方).我相信这是您的主要问题-XML文件中必须只有一个根节点(通常是网站的主页).

And your Mvc.sitemap file would look more like this (with articles being below the home page). I believe this is your main issue - you must only have one root node in your XML file (usually, it is the home page of the site).

<mvcSiteMapNode title="Home" controller="Home" action="Index">
    <mvcSiteMapNode title="About Us" controller="Menu" action="AboutUs">
    <mvcSiteMapNode title="Profile" controller="Menu"   action="Profile">
        <mvcSiteMapNode title="Quality Policy" controller="Menu" action="Policy"/>
    </mvcSiteMapNode>
    <mvcSiteMapNode title="History" controller="Menu" action="History"/>
    <mvcSiteMapNode title="Articles" controller="SiteContents"  action="ArticleIndex" key="Articles"> 
        <mvcSiteMapNode title="Details" dynamicNodeProvider="Myproject.Models.MyDynamicNodeProvider, Myproject"  />
    </mvcSiteMapNode>
</mvcSiteMapNode>

最后,我们有了更正的 DynamicNodeProvider .

Finally, we have the corrected DynamicNodeProvider.

public class MyDynamicNodeProvider
    : DynamicNodeProviderBase
{
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
    { 
        webdata storeDB = new webdata();

        foreach (var article in storeDB.SiteContents) 
        {
            DynamicNode enode = new DynamicNode();
            enode.Title = article.ArticleTitle;
            enode.ParentKey = "Articles";

            // Don't use the Url property unless you have a 
            // non-MVC page/external URL
            //enode.Url = "ArticleDetails/" + article.ArticleID + "/" + article.ArticleAlias;

            // Specify Controller, Action name, and id.
            // These values all must match the request in order 
            // for the node to be considered the "current" node
            enode.Controller = "SiteContents";
            enode.Action = "ArticleDetails";
            enode.RouteValues.Add("id", article.ArticleID);

            // Match the slug (we don't really care what its value is here)
            enode.PreservedRouteParameters.Add("slug");

            yield return enode;
        }
    }
}

这篇关于数据库中的动态站点地图不显示节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆