From 34cb8ea633547ddf6fcffaf0c1f499d8765e5ec2 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Mon, 3 Jun 2013 21:38:43 -1000 Subject: [PATCH] Changed back office routes to use a proper MVC area and ensures that the controller creations for all controllers are limited to ONLY look in the namespaces declared for them, this will increase performance and ensure there's not conflicting controller creation... need to back port this to 6.0.x --- .../Mvc/AreaRegistrationExtensions.cs | 3 ++ src/Umbraco.Web/Mvc/BackOfficeArea.cs | 52 +++++++++++++++++++ src/Umbraco.Web/Umbraco.Web.csproj | 1 + src/Umbraco.Web/WebBootManager.cs | 40 ++++---------- 4 files changed, 66 insertions(+), 30 deletions(-) create mode 100644 src/Umbraco.Web/Mvc/BackOfficeArea.cs diff --git a/src/Umbraco.Web/Mvc/AreaRegistrationExtensions.cs b/src/Umbraco.Web/Mvc/AreaRegistrationExtensions.cs index e418c0cb16..7a1b91adae 100644 --- a/src/Umbraco.Web/Mvc/AreaRegistrationExtensions.cs +++ b/src/Umbraco.Web/Mvc/AreaRegistrationExtensions.cs @@ -86,6 +86,9 @@ namespace Umbraco.Web.Mvc controllerPluginRoute.DataTokens.Add("Namespaces", new[] {controllerType.Namespace}); } + //Don't look anywhere else except this namespace! + controllerPluginRoute.DataTokens.Add("UseNamespaceFallback", false); + //constraints: only match controllers ending with 'controllerSuffixName' and only match this controller's ID for this route controllerPluginRoute.Constraints = new RouteValueDictionary( new Dictionary diff --git a/src/Umbraco.Web/Mvc/BackOfficeArea.cs b/src/Umbraco.Web/Mvc/BackOfficeArea.cs new file mode 100644 index 0000000000..8dc1e9727e --- /dev/null +++ b/src/Umbraco.Web/Mvc/BackOfficeArea.cs @@ -0,0 +1,52 @@ +using System.Web.Mvc; +using Umbraco.Core.Configuration; +using Umbraco.Web.Editors; +using Umbraco.Web.Install; + +namespace Umbraco.Web.Mvc +{ + /// + /// An area registration for back office components + /// + internal class BackOfficeArea : AreaRegistration + { + + /// + /// Create the routes for the area + /// + /// + /// + /// By using the context to register the routes it means that the area is already applied to them all + /// and that the namespaces searched for the controllers are ONLY the ones specified. + /// + public override void RegisterArea(AreaRegistrationContext context) + { + //Default back office route + context.MapRoute( + "Umbraco_back_office", + GlobalSettings.UmbracoMvcArea + "/{action}/{id}", + new {controller = "BackOffice", action = "Default", id = UrlParameter.Optional}, + new[] {typeof (BackOfficeController).Namespace}); + + //Create the install routes + context.MapRoute( + "Umbraco_install_packages", + "Install/PackageInstaller/{action}/{id}", + new {controller = "InstallPackage", action = "Index", id = UrlParameter.Optional}, + new[] {typeof (InstallPackageController).Namespace}); + + //Create the REST/web/script service routes + context.MapRoute( + "Umbraco_web_services", + GlobalSettings.UmbracoMvcArea + "/RestServices/{controller}/{action}/{id}", + new {controller = "SaveFileController", action = "Index", id = UrlParameter.Optional}, + //look in this namespace for controllers + new[] {"Umbraco.Web.WebServices"}); + } + + public override string AreaName + { + get { return GlobalSettings.UmbracoMvcArea; } + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index d5eaf5eda8..b13757d3c6 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -297,6 +297,7 @@ + diff --git a/src/Umbraco.Web/WebBootManager.cs b/src/Umbraco.Web/WebBootManager.cs index d9dacd13b3..d88abe2f27 100644 --- a/src/Umbraco.Web/WebBootManager.cs +++ b/src/Umbraco.Web/WebBootManager.cs @@ -147,19 +147,7 @@ namespace Umbraco.Web protected internal void CreateRoutes() { var umbracoPath = GlobalSettings.UmbracoMvcArea; - - //TODO: We should probably create a 'real' Umbraco area - - //Create the back office route - // TODO: Change this to the normal route, currently it is /Belle for dev testing - var backOfficeRoute = RouteTable.Routes.MapRoute( - "Umbraco_back_office", - //umbracoPath + "/{action}/{id}", - "Belle/{action}/{id}", - new { controller = "BackOffice", action = "Default", id = UrlParameter.Optional }, - new[] { typeof(BackOfficeController).Namespace }); - backOfficeRoute.DataTokens.Add("area", umbracoPath); - + //Create the front-end route var defaultRoute = RouteTable.Routes.MapRoute( "Umbraco_default", @@ -168,27 +156,17 @@ namespace Umbraco.Web ); defaultRoute.RouteHandler = new RenderRouteHandler(ControllerBuilder.Current.GetControllerFactory()); - //Create the install routes - var installPackageRoute = RouteTable.Routes.MapRoute( - "Umbraco_install_packages", - "Install/PackageInstaller/{action}/{id}", - new { controller = "InstallPackage", action = "Index", id = UrlParameter.Optional } - ); - installPackageRoute.DataTokens.Add("area", umbracoPath); + //register all back office routes + RouteBackOfficeControllers(); //plugin controllers must come first because the next route will catch many things RoutePluginControllers(); + } - //Create the REST/web/script service routes - var webServiceRoutes = RouteTable.Routes.MapRoute( - "Umbraco_web_services", - umbracoPath + "/RestServices/{controller}/{action}/{id}", - new { controller = "SaveFileController", action = "Index", id = UrlParameter.Optional }, - //look in this namespace for controllers - new string[] { "Umbraco.Web.WebServices" } - ); - webServiceRoutes.DataTokens.Add("area", umbracoPath); - + private void RouteBackOfficeControllers() + { + var backOfficeArea = new BackOfficeArea(); + RouteTable.Routes.RegisterArea(backOfficeArea); } private void RoutePluginControllers() @@ -241,6 +219,7 @@ namespace Umbraco.Web route.DataTokens = new RouteValueDictionary(); } route.DataTokens.Add("Namespaces", new[] {meta.ControllerNamespace}); //look in this namespace to create the controller + route.DataTokens.Add("UseNamespaceFallback", false); //Don't look anywhere else except this namespace! route.DataTokens.Add("umbraco", "api"); //ensure the umbraco token is set } @@ -255,6 +234,7 @@ namespace Umbraco.Web // 4.10 release so we can't include it now :( new[] { meta.ControllerNamespace }); //look in this namespace to create the controller route.DataTokens.Add("umbraco", "surface"); //ensure the umbraco token is set + route.DataTokens.Add("UseNamespaceFallback", false); //Don't look anywhere else except this namespace! //make it use our custom/special SurfaceMvcHandler route.RouteHandler = new SurfaceRouteHandler(); }