c# - 根据用户角色将 XML 站点地图加载到 MvcSiteMapProvider

标签 c# asp.net mvcsitemapprovider

我已经安装了 MvcSiteMapProvider v4,现在我想动态加载站点地图。我的需求很简单——根据当前登录的用户角色,例如在每个页面请求上加载 XML 站点地图。 AdminSiteMap.xml 和 UserSiteMap.xml

看来这是可以做到的:

所以基本上您需要使用 DI 来实现这一点(矫枉过正恕我直言)。这是否有可能在没有 DI 的情况下完成?

因此,当我使用 ASP Boilerplate ( http://www.aspnetboilerplate.com/ ) 时,我将 CaSTLe Windsor 作为我的 DI。

所以我通过 NuGet 安装了“MvcSiteMapProvider MVC5 Windsor 依赖注入(inject)配置”。但是现在当我运行该应用程序时出现以下错误:

SiteMapLoader 尚未初始化。

Check the 'MvcSiteMapProvider_UseExternalDIContainer' setting in the AppSettings section of web.config.

If the setting is set to 'false', you will need to call the MvcSiteMapProvider.DI.Composer.Compose() method at the end of Application_Start in the Global.asax file. Alternatively, if you are using .NET 4.0 or higher you can install the MvcSiteMapProvider.MVCx NuGet package corresponding to your MVC version.

If the setting is set to 'true', you must set the SiteMaps.Loader property during Application_Start in Global.asax to an instance of the built-in SiteMapLoader type or a custom ISiteMapLoader instance. This can be achieved most easily by using your external DI container.

我没有更改默认配置,并确认在 public class MvcSiteMapProviderInstaller : IWindsorInstaller 中调用了 Install() 方法,因为它在那里遇到了一个断点。

那么我在这里缺少什么来完成这项工作。请记住,我想要做的就是根据每个请求的登录用户加载 SiteMap。

**** 更新 ****

虽然它可能不优雅,但它不需要像实现 DI 容器所建议的那样大量代码。在@ Using Multiple MvcSiteMaps 查看 viggity 的回答(关于第 4 个)

最佳答案

首先,每个用户 1 个 SiteMap 是可能的,但不会很好地扩展 - 事实上它几乎违背了制作 site map 的目的。我不会推荐这种方法,除非你确定你的网站不会有超过几十个并发用户,网站上的页面少于几百个,并且服务器上有大量可用的额外内存。

有更多可扩展的选项可以根据登录的用户使节点可见/不可见。

  • 使用Security Trimming .启用后,只需使用 AuthorizeAttribute 正确配置 MVC 安全性即可自动运行。 . AuthorizeAttribute 完全支持角色。如果需要,您还可以继承 AuthorizeAttribute 以添加自定义安全逻辑。
  • 使用custom visibility providers根据自定义条件控制每个节点是可见还是不可见。
  • 自定义内置 HTML 助手(通过更改 /Views/Shared/DisplayTemplates/ 文件夹中的模板)或 build custom HTML helpers除了来自 SiteMap 实例的链接之外,还可以根据每个用户的请求动态加载链接。

这些方法都不需要外部 DI。

推荐的方法是将所有节点加载到每个用户都可能访问的 SiteMap 中,然后使用安全修整使当前用户的节点在 UI 上不可见。您可以使用 SiteMapCacheReleaseAttribute 在数据更改时强制重新加载缓存。制作dynamic nodes将它们添加到数据源后立即在 UI 上可见。


有了这些知识,如果您仍想继续沿着当前路径前进,则说明您安装了错误的 NuGet 包。依赖注入(inject)的工作方式是您的项目中恰好需要 1 个组合根(即 WindsorContainer 的一个实例)。由于您的项目中已有组合根目录,因此您必须为 Windsor 安装 MvcSiteMapProvider modules only 包,然后通过添加几行代码手动将模块添加到 Windsor 配置中。您可以通过在包管理器控制台中运行此命令来降级到仅模块包:

PM> Uninstall-Package MvcSiteMapProvider.MVC5.DI.Windsor

然后,搜索在您的项目中声明 new WindsorContainer() 的位置,并将 MvcSiteMapProvider 模块添加到您的 DI 配置中。

// Create the DI container (typically part of your DI setup already)
var container = new WindsorContainer();


// Your existing DI configuration should typically be here...

// Setup configuration of DI
container.Install(new MvcSiteMapProviderInstaller()); // Required
container.Install(new MvcInstaller()); // Required by MVC. Typically already part of your setup (double check the contents of the module).

// Setup global sitemap loader (required)
MvcSiteMapProvider.SiteMaps.Loader = container.Resolve<ISiteMapLoader>();

// Check all configured .sitemap files to ensure they follow the XSD for MvcSiteMapProvider (optional)
var validator = container.Resolve<ISiteMapXmlValidator>();
validator.ValidateXml(HostingEnvironment.MapPath("~/Mvc.sitemap"));

// Register the Sitemaps routes for search engines (optional)
XmlSiteMapController.RegisterRoutes(RouteTable.Routes);

如果您确保在整个项目范围内只有 1 个 WindsorConntainer 实例并适当添加上面的代码,您应该有一个有效的 DI 配置。

要为每个用户加载 1 个 SiteMap,您需要创建一个自定义 ISiteMapCacheKeyGenerator,它会为每个用户返回不同的字符串。

public class UserSiteMapCacheKeyGenerator
    : ISiteMapCacheKeyGenerator
{
    public virtual string GenerateKey()
    {
        var context = HttpContext.Current;
        if (context.User.Identity.IsAuthenticated)
        {
            // Note: the way you retrieve the user name depends on whether you are using 
            // Windows or Forms authentication
            return context.User.Identity.Name;
        }
        else
        {
            return "default";
        }
    }
}

并通过在 /DI/Windsor/Installers/MvcSiteMapProviderInstaller.cs 编辑 Windsor 模块来注入(inject)它。

var excludeTypes = new Type[] { 
    // Use this array to add types you wish to explicitly exclude from convention-based  
    // auto-registration. By default all types that either match I[TypeName] = [TypeName] or 
    // I[TypeName] = [TypeName]Adapter will be automatically wired up as long as they don't 
    // have the [ExcludeFromAutoRegistrationAttribute].
    //
    // If you want to override a type that follows the convention, you should add the name 
    // of either the implementation name or the interface that it inherits to this list and 
    // add your manual registration code below. This will prevent duplicate registrations 
    // of the types from occurring. 

    // Example:
    // typeof(SiteMap),
    // typeof(SiteMapNodeVisibilityProviderStrategy)
    typeof(SiteMapNodeUrlResolver),
    typeof(ISiteMapCacheKeyGenerator) // <-- add this line
};

// Code omitted here...


// Add this to the bottom of the module
container.Register(Component.For<ISiteMapCacheKeyGenerator>().ImplementedBy<UserSiteMapCacheKeyGenerator>();

唯一剩下的就是使用 dynamic node providers或 ISiteMapNodeProvider 的实现,以动态地为每个用户提供节点。如上设置的话,可以通过SiteMap.CacheKey属性获取用户名。

public class SomeDynamicNodeProvider : DynamicNodeProviderBase
{
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
    {
        // Get the user name
        var user = node.SiteMap.CacheKey;

        // Entities would be your entity framework context class
        // or repository.
        using (var entities = new Entities())
        {
            // Add the nodes for the current user only
            foreach (var story in entities.Stories.Where(x => x.User == user)
            {
                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;

                // If you have child nodes to the current node, you can
                // nest them here by setting their ParentKey property to 
                // the same value as the dynamicNode.Key and returning them
                // using yield return.
            }
        }
    }
}

最后,将您的"template"节点添加到您的配置中以加载动态节点。

// Set a key explicitly to attach the dynamic nodes to. 
// The key property here corresponds to the ParentKey property of the dynamic node.
<mvcSiteMapNode title="Home" controller="Home" action="Index" key="Home">
    // Use a "dummy" node for each dynamic node provider. This node won't be in the SiteMap.
    <mvcSiteMapNode dynamicNodeProvider="NamespaceName.SomeDynamicNodeProivder, AssemblyName"/>
</mvcSiteMapNode>

关于c# - 根据用户角色将 XML 站点地图加载到 MvcSiteMapProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26541338/

相关文章:

C# 创建新 T()

c# - MVC SiteMap 从 menuhelper 中隐藏一个节点,但在 sitepathhelper 中显示(面包屑)

asp.net-mvc - MVCSiteMapProvider - 显示父节点的标题

asp.net-mvc-4 - MvcSiteMap 提供程序中的自定义属性

c# - 如何在 Visual Studio 2008/2010 中捕获并保存解决方案项目的编译时和运行时错误

c# - 为什么我不能在同一个类上使用 WCF DataContract 和 ISerializable?

c# - 检查当前线程是否等于构造对象的线程

c# - 如何在c#中转换表达式

asp.net - 如何使用 Wkhtmltopdf 转换器填充文本框?

c# - Azure 网站错误 502