From fe40bbf8e09886cda503d3dbc30cc99f16d343ac Mon Sep 17 00:00:00 2001 From: Shad Storhaug Date: Sat, 6 Apr 2013 23:30:47 +0700 Subject: [PATCH] Fixes #131, problems interoperating with ELMAH. Added configuration setting to exclude namespaces from controller type resolution. --- src/MvcSiteMapProvider/MvcMusicStore/Global.asax.cs | 13 +++++++++---- .../MvcSiteMapProvider/DI/ConfigurationSettings.cs | 3 +++ .../DI/SiteMapFactoryContainer.cs | 4 ++++ .../MvcSiteMapProvider/DI/SiteMapLoaderContainer.cs | 2 +- .../Web/Mvc/ControllerTypeResolver.cs | 11 ++++++++++- .../Web/Mvc/ControllerTypeResolverFactory.cs | 9 ++++++++- 6 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/MvcSiteMapProvider/MvcMusicStore/Global.asax.cs b/src/MvcSiteMapProvider/MvcMusicStore/Global.asax.cs index 3da030df..db3c2e66 100644 --- a/src/MvcSiteMapProvider/MvcMusicStore/Global.asax.cs +++ b/src/MvcSiteMapProvider/MvcMusicStore/Global.asax.cs @@ -26,13 +26,18 @@ protected void Application_Start() FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); // MvcSiteMapProvider Configuration -#if NET35 +//#if NET35 MvcSiteMapProvider.DI.Composer.Compose(); -#endif +//#endif XmlSiteMapController.RegisterRoutes(RouteTable.Routes); - var container = DIConfig.Register(); - MvcSiteMapProviderConfig.Register(container); + // NOTE: This check wouldn't have to be made in a real-world application - we do it + // in the demo because we want to support both the internal and external DI containers. + if (new MvcSiteMapProvider.DI.ConfigurationSettings().UseExternalDIContainer == true) + { + var container = DIConfig.Register(); + MvcSiteMapProviderConfig.Register(container); + } } /// diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/DI/ConfigurationSettings.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/DI/ConfigurationSettings.cs index a898843d..f4bebfa9 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/DI/ConfigurationSettings.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/DI/ConfigurationSettings.cs @@ -22,6 +22,8 @@ public ConfigurationSettings() this.AttributesToIgnore = GetConfigurationValueOrFallback("MvcSiteMapProvider_AttributesToIgnore", "") .Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList(); this.CacheDuration = int.Parse(GetConfigurationValueOrFallback("MvcSiteMapProvider_CacheDuration", "5")); + this.ExcludeNamespacesForResolver = GetConfigurationValueOrFallback("MvcSiteMapProvider_ExcludeNamespacesForResolver", "") + .Split(new char[] { '|', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList(); } public bool UseExternalDIContainer { get; private set; } @@ -31,6 +33,7 @@ public ConfigurationSettings() public IEnumerable IncludeAssembliesForScan { get; private set; } public IEnumerable AttributesToIgnore { get; private set; } public int CacheDuration { get; private set; } + public IEnumerable ExcludeNamespacesForResolver { get; private set; } private string GetConfigurationValueOrFallback(string name, string defaultValue) diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/DI/SiteMapFactoryContainer.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/DI/SiteMapFactoryContainer.cs index fa5c6f1a..c46cb01f 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/DI/SiteMapFactoryContainer.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/DI/SiteMapFactoryContainer.cs @@ -20,14 +20,17 @@ namespace MvcSiteMapProvider.DI public class SiteMapFactoryContainer { public SiteMapFactoryContainer( + ConfigurationSettings settings, IMvcContextFactory mvcContextFactory, IUrlPath urlPath) { + this.settings = settings; this.mvcContextFactory = mvcContextFactory; this.requestCache = this.mvcContextFactory.GetRequestCache(); this.urlPath = urlPath; } + private readonly ConfigurationSettings settings; private readonly IMvcContextFactory mvcContextFactory; private readonly IRequestCache requestCache; private readonly IUrlPath urlPath; @@ -82,6 +85,7 @@ private ISiteMapChildStateFactory ResolveSiteMapChildStateFactory() private IControllerTypeResolverFactory ResolveControllerTypeResolverFactory() { return new ControllerTypeResolverFactory( + settings.ExcludeNamespacesForResolver, new ControllerBuilderAdaptor(ControllerBuilder.Current), new BuildManagerAdaptor() ); diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/DI/SiteMapLoaderContainer.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/DI/SiteMapLoaderContainer.cs index d8847dbe..f9b2e006 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/DI/SiteMapLoaderContainer.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/DI/SiteMapLoaderContainer.cs @@ -37,7 +37,7 @@ public SiteMapLoaderContainer(ConfigurationSettings settings) this.siteMapXmlNameProvider = new SiteMapXmlNameProvider(); this.dynamicNodeBuilder = new DynamicNodeBuilder(this.nodeKeyGenerator, this.siteMapNodeFactory); this.siteMapBuiderSetStrategy = this.ResolveSiteMapBuilderSetStrategy(settings); - var siteMapFactoryContainer = new SiteMapFactoryContainer(this.mvcContextFactory, this.urlPath); + var siteMapFactoryContainer = new SiteMapFactoryContainer(settings, this.mvcContextFactory, this.urlPath); this.siteMapFactory = siteMapFactoryContainer.ResolveSiteMapFactory(); this.siteMapCreator = new SiteMapCreator(this.siteMapCacheKeyToBuilderSetMapper, this.siteMapBuiderSetStrategy, this.siteMapFactory); } diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/ControllerTypeResolver.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/ControllerTypeResolver.cs index 7a176189..3da3bd62 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/ControllerTypeResolver.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/ControllerTypeResolver.cs @@ -16,11 +16,14 @@ public class ControllerTypeResolver : IControllerTypeResolver { public ControllerTypeResolver( + IEnumerable areaNamespacesToIgnore, RouteCollection routes, IControllerBuilder controllerBuilder, IBuildManager buildManager ) { + if (areaNamespacesToIgnore == null) + throw new ArgumentNullException("areaNamespacesToIgnore"); if (routes == null) throw new ArgumentNullException("routes"); if (controllerBuilder == null) @@ -28,6 +31,7 @@ IBuildManager buildManager if (buildManager == null) throw new ArgumentNullException("buildManager"); + this.areaNamespacesToIgnore = areaNamespacesToIgnore; this.routes = routes; this.controllerBuilder = controllerBuilder; this.buildManager = buildManager; @@ -35,6 +39,7 @@ IBuildManager buildManager Cache = new Dictionary(); } + protected readonly IEnumerable areaNamespacesToIgnore; protected readonly RouteCollection routes; protected readonly IControllerBuilder controllerBuilder; protected readonly IBuildManager buildManager; @@ -73,7 +78,11 @@ public Type ResolveControllerType(string areaName, string controllerName) } // Find controller details - IEnumerable areaNamespaces = FindNamespacesForArea(areaName, this.routes); + IEnumerable areaNamespaces = (from ns in FindNamespacesForArea(areaName, this.routes) + where ns != "Elmah.Mvc" + where !this.areaNamespacesToIgnore.Contains(ns) + select ns).ToList(); + string area = areaName; string controller = controllerName; diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/ControllerTypeResolverFactory.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/ControllerTypeResolverFactory.cs index 2648c87b..26060da7 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/ControllerTypeResolverFactory.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/Mvc/ControllerTypeResolverFactory.cs @@ -1,5 +1,7 @@ using System; using System.Web.Mvc; +using System; +using System.Collections.Generic; using System.Web.Routing; using MvcSiteMapProvider.Web.Compilation; @@ -13,19 +15,24 @@ public class ControllerTypeResolverFactory : IControllerTypeResolverFactory { public ControllerTypeResolverFactory( + IEnumerable areaNamespacesToIgnore, IControllerBuilder controllerBuilder, IBuildManager buildManager ) { + if (areaNamespacesToIgnore == null) + throw new ArgumentNullException("areaNamespacesToIgnore"); if (controllerBuilder == null) throw new ArgumentNullException("controllerBuilder"); if (buildManager == null) throw new ArgumentNullException("buildManager"); + this.areaNamespacesToIgnore = areaNamespacesToIgnore; this.controllerBuilder = controllerBuilder; this.buildManager = buildManager; } + protected readonly IEnumerable areaNamespacesToIgnore; protected readonly IControllerBuilder controllerBuilder; protected readonly IBuildManager buildManager; @@ -33,7 +40,7 @@ IBuildManager buildManager public IControllerTypeResolver Create(RouteCollection routes) { - return new ControllerTypeResolver(routes, controllerBuilder, buildManager); + return new ControllerTypeResolver(areaNamespacesToIgnore, routes, controllerBuilder, buildManager); } #endregion