- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经安装了 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 的目的。我不会推荐这种方法,除非你确定你的网站不会有超过几十个并发用户,网站上的页面少于几百个,并且服务器上有大量可用的额外内存。
有更多可扩展的选项可以根据登录的用户使节点可见/不可见。
/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/
我想要显示正在加载的 .gif,直到所有内容都已加载,包括嵌入的 iframe。但是,目前加载 gif 会在除 iframe 之外的所有内容都已加载后消失。我怎样才能让它等到 iframe 也加载完毕
首先,这是我第一次接触 Angular。 我想要实现的是,我有一个通知列表,我必须以某种方式限制 limitTo,因此元素被限制为三个,在我单击按钮后,其余的应该加载。 我不明白该怎么做: 设置“ V
我正在尝试在我的设备上运行这个非常简单的应用程序(使用 map API V2),并且出于某种原因尝试使用 MapView 时: 使用 java 文件: public class MainMap e
我正在使用 Python 2.6、Excel 2007 Professional 和最新版本的 PyXLL。在 PyXLL 中加载具有 import scipy 抛出异常,模块未加载。有没有人能够在
我想做这个: 创建并打包原始游戏。然后我想根据原始游戏中的蓝图创建具有新网格/声音/动画和蓝图的其他 PAK 文件。原始游戏不应该知道有关其他网格/动画/等的任何信息。因此,我需要在原始游戏中使用 A
**摘要:**在java项目中经常会使用到配置文件,这里就介绍几种加载配置文件的方法。 本文分享自华为云社区《【Java】读取/加载 properties配置文件的几种方法》,作者:Copy工程师。
在 Groovy 脚本中是否可以执行条件导入语句? if (test){ import this.package.class } else { import that.package.
我正在使用 NVidia 视觉分析器(来自 CUDA 5.0 beta 版本的基于 eclipse 的版本)和 Fermi 板,我不了解其中两个性能指标: 全局加载/存储效率表示实际内存事务数与请求事
有没有办法在通过 routeProvider 加载特定 View 时清除 Angular JS 存储的历史记录? ? 我正在使用 Angular 创建一个公共(public)安装,并且历史会积累很多,
使用 Xcode 4.2,在我的应用程序中, View 加载由 segue 事件触发。 在 View Controller 中首先调用什么方法? -(void) viewWillAppear:(BOO
我在某些Django模型中使用JSONField,并希望将此数据从Oracle迁移到Postgres。 到目前为止,当使用Django的dumpdata和loaddata命令时,我仍然没有运气来保持J
创建 Nib 时,我需要创建两种类型:WindowNib 或 ViewNib。我看到的区别是,窗口 Nib 有一个窗口和一个 View 。 如何将 View Nib 加载到另一个窗口中?我是否必须创建
我想将多个env.variables转换为静态结构。 我可以手动进行: Env { is_development: env::var("IS_DEVELOPMENT")
正如我从一个测试用例中看到的:https://godbolt.org/z/K477q1 生成的程序集加载/存储原子松弛与普通变量相同:ldr 和 str 那么,宽松的原子变量和普通变量之间有什么区别吗
我有一个重定向到外部网站的按钮/链接,但是外部网站需要一些时间来加载。所以我想添加一个加载屏幕,以便外部页面在显示之前完全加载。我无法控制外部网站,并且外部网站具有同源策略,因此我无法在 iFrame
我正在尝试为我的应用程序开发一个Dockerfile,该文件在初始化后加载大量环境变量。不知何故,当我稍后执行以下命令时,这些变量是不可用的: docker exec -it container_na
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我刚刚遇到一个问题,我有一个带有一些不同选项的选择标签。 现在我想检查用户选择了哪些选项。 然后我想将一个新的 html 文件加载到该网站(取决于用户选中的选项)宽度 javascript,我该怎么做
我知道两种保存/加载应用程序设置的方法: 使用PersistentStore 使用文件系统(存储,因为 SDCard 是可选的) 我想知道您使用应用程序设置的做法是什么? 使用 PersistentS
我开始使用 Vulkan 时偶然发现了我的第一个问题。尝试创建调试报告回调时(验证层和调试扩展在我的英特尔 hd vulkan 驱动程序上可用,至少它是这么说的),它没有告诉我 vkCreateDeb
我是一名优秀的程序员,十分优秀!