【问题标题】:Ignoring RouteValues with MvcSitemapprovider MVC 5 Unity使用 MvcSitemapprovider MVC 5 Unity 忽略 RouteValues
【发布时间】:2014-12-04 00:39:36
【问题描述】:

我有一个使用来自 NuGet 的 MvcSiteMapProvider 包的带有 Unity DI 的 MVC 5 应用程序。我正在尝试在我的web.config 文件中使用忽略MvcSiteMapProvider_AttributesToIgnore appSetting,如下所示

<add key="MvcSiteMapProvider_AttributesToIgnore" value="type" />

但是当显示我的站点地图时,url 具有被忽略的值作为查询字符串。

MvcSiteMapProvider 文档中有一段内容如下:

如果使用外部 DI 容器,此设置可以在 ReservedAttributeNameProvider 的构造函数中名为“attributesToIgnore”的参数中找到,该参数为 IEnumerable 类型

我正在使用 Unity,所以我想知道是否需要以某种方式将其添加到我的容器中,但我不知道如何。

关于如何忽略路由值的任何想法?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-5 unity-container mvcsitemapprovider


    【解决方案1】:

    您只需按照文档中的说明将设置添加到 ReservedAttributeNameProvider:

    // Prepare for the sitemap node providers
    this.Container.RegisterType<IXmlSource, FileXmlSource>("file1XmlSource", new InjectionConstructor(absoluteFileName));
    this.Container.RegisterType<IReservedAttributeNameProvider, ReservedAttributeNameProvider>(
        // Add the attributes to ignore here
        new InjectionConstructor(new string[] { "type", "value2" }));
    

    提示:您始终可以通过临时更新类并使用 Intellisense 查看参数来找出类具有哪些依赖项。

    new ReservedAttributeNameProvider( 
    
    // Type the above in Visual Studio and you will see the constructor parameters
    // in a tooltip
    

    【讨论】:

    • 成功了。我试图将其添加到错误的位置。我忘了提到我安装了 MvcSiteMapProvider MVC5 Unity 模块,在这种情况下,我应该尝试在 MvcSiteMapProviderContainerExtension 类中进行此更改。谢谢。
    • 我可能需要启动一个新线程,但是当我添加我的属性以忽略 SiteMapNodeModel.IsCurrentNode 和 SiteMapNodeModel.IsInCurrentPath 时,它们在具有要忽略的属性集的节点上应该为 true 时为 false。如果忽略要忽略的属性,这些属性将按预期工作。有什么建议吗?
    • 在 AttributesToIgnore 中设置键时,它将不再是路由匹配的一部分。这就是它的目的 - 这样您就可以将 not 的属性设置为路由值。听起来您正在尝试排除应该包含的值以进行路由匹配。不过,如果您仍然对此感到困惑,那么提出一个新问题可能是值得的。
    • 请注意:如果使用 Castle Windsor,您可以在 MvcSiteMapProviderInstaller 中设置这些设置。在这里工作得很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 2022-01-22
    • 1970-01-01
    相关资源
    最近更新 更多