使用MvcSiteMapProvider是否可以以编程方式在索引范围之间注册很多页面? [英] With MvcSiteMapProvider Is it possible to programmatically register lots of pages between index ranges?

查看:70
本文介绍了使用MvcSiteMapProvider是否可以以编程方式在索引范围之间注册很多页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 MVCSiteMapProvider ,但我在文档中找不到任何可以允许我进行以下操作的内容按索引注册大量网址.我有以下

I'm looking at MVCSiteMapProvider but I can't find anything in documentation that would allow me to register lots of urls by index. I have the following

http://example.com/story/1
...
...
http://example.com/story/7000000

我希望能够使用该库自动将它们提供给许多不同的文件.我已经阅读了所有文档,但找不到任何东西.不过,它似乎确实功能齐全,因此我想我会在推出自己的解决方案之前先询问一下.

I'd like to be able to use the library to automatically serve these in lots of different files. I've read through all the documentation but can't find anything. It seems really fully featured though so I thought I would ask before rolling my own solution.

推荐答案

您可以使用动态节点提供程序或实施ISiteMapNodeProvider以编程方式从任何来源提供您自己的数据(包括自定义ID).

You can use a dynamic node provider or implement ISiteMapNodeProvider to programatically supply your own data (including custom ids) from any source.

可以在不使用外部依赖项注入容器的情况下添加动态节点提供程序,但是您需要以XML或使用.NET属性添加模板"节点,以将提供程序附加到该节点上(请参见上面的链接).

Dynamic node providers can be added without using an external dependency injection container, but you need to add a "template" node either in XML or using .NET attributes to attach the provider to (see the above link).

public class StoryDynamicNodeProvider : DynamicNodeProviderBase
{
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
    {
        // Entities would be your entity framework context class
        // or repository.
        using (var entities = new Entities())
        {
            // Create a node for each blog post
            foreach (var story in entities.Stories)
            {
                DynamicNode dynamicNode = new DynamicNode();
                dynamicNode.Title = story.Title;

                // The key of the node that this node will be the child of.
                // This works best if you explicitly set the key property/attribute 
                // of the parent node.
                dynamicNode.ParentKey = "Home"; 
                dynamicNode.Key = "Story_" + story.Id;
                dynamicNode.Controller = "Story";
                dynamicNode.Action = "Details";

                // Add the "id" (or any other custom route values)
                dynamicNode.RouteValues.Add("id", story.Id);

                yield return dynamicNode;
            }
        }
    }
}

使用ISiteMapNodeProvider可以构建包括根节点在内的整个SiteMap结构,但是当前它需要使用外部DI容器来注入自定义实现.

Using ISiteMapNodeProvider you can build the entire SiteMap structure including the root node, but currently it requires using an external DI container to inject a custom implementation.

此处示例自己实现ISiteMapNodeProvider.这是使用SimpleInjector注入自定义实现的示例.

There is an example here of how you could implement ISiteMapNodeProvider yourself. Here is an example of injecting a custom implementation using SimpleInjector.

请注意,由于服务器上的节点总数已缓存在内存中,因此目前对服务器上的节点总数有十万个限制,因此,如果有那么多节点,则使用preparedRouteParameters是一个更好的选择.但是,它有一个局限性,即单个URL只能出现在SiteMapPath中,而不能出现在搜索引擎的Menu,SiteMap或XML Sitemap中.

Do note that there is currently a limitation in the 10s of thousands for the total number of nodes on a server because they are cached in memory, so if you have that many nodes using preservedRouteParameters is a better choice. However, it has a limitation that the individual URLs can only appear in the SiteMapPath, but not in the Menu, SiteMap, or XML Sitemap for search engines.

这篇关于使用MvcSiteMapProvider是否可以以编程方式在索引范围之间注册很多页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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