diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index f614ac6130..65c1b59528 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -198,7 +198,6 @@ - @@ -217,7 +216,6 @@ - diff --git a/src/Umbraco.Tests/Web/Controllers/PluginControllerAreaTests.cs b/src/Umbraco.Tests/Web/Controllers/PluginControllerAreaTests.cs deleted file mode 100644 index a91a305314..0000000000 --- a/src/Umbraco.Tests/Web/Controllers/PluginControllerAreaTests.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System; -using NUnit.Framework; -using Umbraco.Tests.TestHelpers; -using Umbraco.Web; -using Umbraco.Web.Mvc; - -namespace Umbraco.Tests.Web.Controllers -{ - [TestFixture] - public class PluginControllerAreaTests : BaseWebTest - { - - [Test] - public void Ensure_Same_Area1() - { - Assert.Throws(() => - new PluginControllerArea(TestObjects.GetGlobalSettings(), HostingEnvironment, - new PluginControllerMetadata[] - { - PluginController.GetMetadata(typeof(Plugin1Controller)), - PluginController.GetMetadata(typeof(Plugin2Controller)), - PluginController.GetMetadata(typeof(Plugin3Controller)) //not same area - })); - } - - [Test] - public void Ensure_Same_Area3() - { - Assert.Throws(() => - new PluginControllerArea(TestObjects.GetGlobalSettings(), HostingEnvironment, - new PluginControllerMetadata[] - { - PluginController.GetMetadata(typeof(Plugin1Controller)), - PluginController.GetMetadata(typeof(Plugin2Controller)), - PluginController.GetMetadata(typeof(Plugin4Controller)) //no area assigned - })); - } - - [Test] - public void Ensure_Same_Area2() - { - var area = new PluginControllerArea(TestObjects.GetGlobalSettings(), HostingEnvironment, - new PluginControllerMetadata[] - { - PluginController.GetMetadata(typeof(Plugin1Controller)), - PluginController.GetMetadata(typeof(Plugin2Controller)) - }); - Assert.Pass(); - } - - #region Test classes - - [PluginController("Area1")] - public class Plugin1Controller : PluginController - { - public Plugin1Controller(IUmbracoContextAccessor umbracoContextAccessor) - : base(umbracoContextAccessor, null, null, null, null) - { - } - } - - [PluginController("Area1")] - public class Plugin2Controller : PluginController - { - public Plugin2Controller(IUmbracoContextAccessor umbracoContextAccessor) - : base(umbracoContextAccessor, null, null, null, null) - { - } - } - - [PluginController("Area2")] - public class Plugin3Controller : PluginController - { - public Plugin3Controller(IUmbracoContextAccessor umbracoContextAccessor) - : base(umbracoContextAccessor, null, null, null, null) - { - } - } - - public class Plugin4Controller : PluginController - { - public Plugin4Controller(IUmbracoContextAccessor umbracoContextAccessor) - : base(umbracoContextAccessor, null, null, null, null) - { - } - } - - #endregion - - } -} diff --git a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs b/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs deleted file mode 100644 index 09748e9621..0000000000 --- a/src/Umbraco.Tests/Web/Mvc/SurfaceControllerTests.cs +++ /dev/null @@ -1,221 +0,0 @@ -using System; -using System.Threading.Tasks; -using System.Web; -using System.Web.Mvc; -using System.Web.Routing; -using Moq; -using NUnit.Framework; -using Umbraco.Core.Cache; -using Umbraco.Core.Configuration.Models; -using Umbraco.Core.Models.PublishedContent; -using Umbraco.Core.Services; -using Umbraco.Tests.Common; -using Umbraco.Tests.TestHelpers; -using Umbraco.Tests.Testing; -using Umbraco.Web; -using Umbraco.Web.Mvc; -using Umbraco.Web.PublishedCache; -using Current = Umbraco.Web.Composing.Current; - -namespace Umbraco.Tests.Web.Mvc -{ - [TestFixture] - [UmbracoTest(WithApplication = true)] - public class SurfaceControllerTests : UmbracoTestBase - { - - public override void SetUp() - { - base.SetUp(); - Current.UmbracoContextAccessor = new TestUmbracoContextAccessor(); - } - - [Test] - public void Can_Construct_And_Get_Result() - { - var globalSettings = TestObjects.GetGlobalSettings(); - var httpContextAccessor = TestHelper.GetHttpContextAccessor(); - - var umbracoContextFactory = new UmbracoContextFactory( - Current.UmbracoContextAccessor, - Mock.Of(), - new TestVariationContextAccessor(), - new TestDefaultCultureAccessor(), - globalSettings, - Mock.Of(), - HostingEnvironment, - UriUtility, - httpContextAccessor, - new AspNetCookieManager(httpContextAccessor)); - - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); - var umbracoContext = umbracoContextReference.UmbracoContext; - - var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); - - var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of()); - - var result = ctrl.Index(); - - Assert.IsNotNull(result); - } - - [Test] - public void Umbraco_Context_Not_Null() - { - var globalSettings = TestObjects.GetGlobalSettings(); - var httpContextAccessor = TestHelper.GetHttpContextAccessor(); - - var umbracoContextFactory = new UmbracoContextFactory( - Current.UmbracoContextAccessor, - Mock.Of(), - new TestVariationContextAccessor(), - new TestDefaultCultureAccessor(), - globalSettings, - Mock.Of(), - HostingEnvironment, - UriUtility, - httpContextAccessor, - new AspNetCookieManager(httpContextAccessor)); - - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); - var umbCtx = umbracoContextReference.UmbracoContext; - - var umbracoContextAccessor = new TestUmbracoContextAccessor(umbCtx); - - var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of()); - - Assert.IsNotNull(ctrl.UmbracoContext); - } - - [Test] - public void Can_Lookup_Content() - { - var publishedSnapshot = new Mock(); - publishedSnapshot.Setup(x => x.Members).Returns(Mock.Of()); - var content = new Mock(); - content.Setup(x => x.Id).Returns(2); - var publishedSnapshotService = new Mock(); - var globalSettings = TestObjects.GetGlobalSettings(); - var httpContextAccessor = TestHelper.GetHttpContextAccessor(); - - var umbracoContextFactory = new UmbracoContextFactory( - Current.UmbracoContextAccessor, - publishedSnapshotService.Object, - new TestVariationContextAccessor(), - new TestDefaultCultureAccessor(), - globalSettings, - Mock.Of(), - HostingEnvironment, - UriUtility, - httpContextAccessor, - new AspNetCookieManager(httpContextAccessor)); - - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); - var umbracoContext = umbracoContextReference.UmbracoContext; - - var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); - - var publishedContentQuery = Mock.Of(query => query.Content(2) == content.Object); - - var ctrl = new TestSurfaceController(umbracoContextAccessor,publishedContentQuery); - var result = ctrl.GetContent(2) as PublishedContentResult; - - Assert.IsNotNull(result); - Assert.IsNotNull(result.Content); - Assert.AreEqual(2, result.Content.Id); - } - - [Test] - public async Task Mock_Current_Page() - { - var globalSettings = TestObjects.GetGlobalSettings(); - var httpContextAccessor = TestHelper.GetHttpContextAccessor(); - - var umbracoContextFactory = new UmbracoContextFactory( - Current.UmbracoContextAccessor, - Mock.Of(), - new TestVariationContextAccessor(), - new TestDefaultCultureAccessor(), - globalSettings, - Mock.Of(), - HostingEnvironment, - UriUtility, - httpContextAccessor, - new AspNetCookieManager(httpContextAccessor)); - - var umbracoContextReference = umbracoContextFactory.EnsureUmbracoContext(); - var umbracoContext = umbracoContextReference.UmbracoContext; - - var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); - - var content = Mock.Of(publishedContent => publishedContent.Id == 12345); - - var webRoutingSettings = new WebRoutingSettings(); - var publishedRouter = BaseWebTest.CreatePublishedRouter(umbracoContextAccessor, webRoutingSettings); - var frequest = await publishedRouter.CreateRequestAsync(new Uri("http://localhost/test")); - frequest.SetPublishedContent(content); - - var routeDefinition = new RouteDefinition - { - PublishedRequest = frequest.Build() - }; - - var routeData = new RouteData(); - routeData.Values.Add(Core.Constants.Web.UmbracoRouteDefinitionDataToken, routeDefinition); - - var ctrl = new TestSurfaceController(umbracoContextAccessor, Mock.Of()); - ctrl.ControllerContext = new ControllerContext(Mock.Of(), routeData, ctrl); - - var result = ctrl.GetContentFromCurrentPage() as PublishedContentResult; - - Assert.AreEqual(12345, result.Content.Id); - } - - public class TestSurfaceController : SurfaceController - { - private readonly IPublishedContentQuery _publishedContentQuery; - - public TestSurfaceController(IUmbracoContextAccessor umbracoContextAccessor, IPublishedContentQuery publishedContentQuery) - : base(umbracoContextAccessor, null, ServiceContext.CreatePartial(), AppCaches.Disabled, null) - { - _publishedContentQuery = publishedContentQuery; - } - - public ActionResult Index() - { - // ReSharper disable once Mvc.ViewNotResolved - return View(); - } - - public ActionResult GetContent(int id) - { - var content = _publishedContentQuery.Content(id); - - return new PublishedContentResult(content); - } - - public ActionResult GetContentFromCurrentPage() - { - var content = CurrentPage; - - return new PublishedContentResult(content); - } - } - - public class PublishedContentResult : ActionResult - { - public IPublishedContent Content { get; set; } - - public PublishedContentResult(IPublishedContent content) - { - Content = content; - } - - public override void ExecuteResult(ControllerContext context) - { - } - - } - } -} diff --git a/src/Umbraco.Web/Composing/ModuleInjector.cs b/src/Umbraco.Web/Composing/ModuleInjector.cs deleted file mode 100644 index 5cfec1c484..0000000000 --- a/src/Umbraco.Web/Composing/ModuleInjector.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System.Web; -using Microsoft.Extensions.DependencyInjection; -using Umbraco.Core; -using Umbraco.Core.Composing; -using Umbraco.Core.Exceptions; - -namespace Umbraco.Web.Composing -{ - /// - /// Provides a base class for module injectors. - /// - /// The type of the injected module. - public abstract class ModuleInjector : IHttpModule - where TModule : class, IHttpModule - { - protected TModule Module { get; private set; } - - /// - public void Init(HttpApplication context) - { - try - { - // using the service locator here - no other way, really - Module = Current.Factory.GetRequiredService(); - } - catch - { - // if GetInstance fails, it may be because of a boot error, in - // which case that is the error we actually want to report - IRuntimeState runtimeState = null; - - try - { - runtimeState = Current.Factory.GetRequiredService(); - } - catch { /* don't make it worse */ } - - if (runtimeState?.BootFailedException != null) - BootFailedException.Rethrow(runtimeState.BootFailedException); - - // else... throw what we have - throw; - } - - // initialize - Module.Init(context); - } - - /// - public void Dispose() - { - Module?.Dispose(); - } - } -} diff --git a/src/Umbraco.Web/Mvc/PluginControllerArea.cs b/src/Umbraco.Web/Mvc/PluginControllerArea.cs deleted file mode 100644 index a4440ec4a6..0000000000 --- a/src/Umbraco.Web/Mvc/PluginControllerArea.cs +++ /dev/null @@ -1,104 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web.Mvc; -using System.Web.Routing; -using Umbraco.Core; -using Umbraco.Core.Configuration; -using Umbraco.Core.Composing; -using Umbraco.Core.Configuration.Models; -using Umbraco.Core.Hosting; -using Umbraco.Core.IO; -using Umbraco.Web.WebApi; - -namespace Umbraco.Web.Mvc -{ - /// - /// A custom area for controllers that are plugins - /// - internal class PluginControllerArea : AreaRegistration - { - private readonly GlobalSettings _globalSettings; - private readonly IHostingEnvironment _hostingEnvironment; - private readonly IEnumerable _surfaceControllers; - private readonly IEnumerable _apiControllers; - private readonly string _areaName; - - /// - /// The constructor accepts all types of plugin controllers and will verify that ALL of them have the same areaName assigned to them - /// based on their PluginControllerAttribute. If they are not the same an exception will be thrown. - /// - /// - /// - /// - public PluginControllerArea(GlobalSettings globalSettings, IHostingEnvironment hostingEnvironment, IEnumerable pluginControllers) - { - _globalSettings = globalSettings; - _hostingEnvironment = hostingEnvironment; - var controllers = pluginControllers.ToArray(); - - if (controllers.Any(x => x.AreaName.IsNullOrWhiteSpace())) - { - throw new InvalidOperationException("Cannot create a PluginControllerArea unless all plugin controllers assigned have a PluginControllerAttribute assigned"); - } - _areaName = controllers.First().AreaName; - foreach(var c in controllers) - { - if (c.AreaName != _areaName) - { - throw new InvalidOperationException("Cannot create a PluginControllerArea unless all plugin controllers assigned have the same AreaName. The first AreaName found was " + _areaName + " however, the controller of type " + c.GetType().FullName + " has an AreaName of " + c.AreaName); - } - } - - //get the controllers - _surfaceControllers = controllers.Where(x => TypeHelper.IsTypeAssignableFrom(x.ControllerType)); - _apiControllers = controllers.Where(x => TypeHelper.IsTypeAssignableFrom(x.ControllerType)); - } - - public override void RegisterArea(AreaRegistrationContext context) - { - MapRouteSurfaceControllers(context.Routes, _surfaceControllers); - MapRouteApiControllers(context.Routes, _apiControllers); - } - - public override string AreaName - { - get { return _areaName; } - } - - /// - /// Registers all surface controller routes - /// - /// - /// - /// - /// The routes will be: - /// - /// /Umbraco/[AreaName]/[ControllerName]/[Action]/[Id] - /// - private void MapRouteSurfaceControllers(RouteCollection routes, IEnumerable surfaceControllers) - { - foreach (var s in surfaceControllers) - { - var route = this.RouteControllerPlugin(_globalSettings, _hostingEnvironment, s.ControllerName, s.ControllerType, routes, "", "Index", UrlParameter.Optional, "surface"); - //set the route handler to our SurfaceRouteHandler - route.RouteHandler = new SurfaceRouteHandler(); - } - } - - /// - /// Registers all api controller routes - /// - /// - /// - private void MapRouteApiControllers(RouteCollection routes, IEnumerable apiControllers) - { - foreach (var s in apiControllers) - { - this.RouteControllerPlugin(_globalSettings, _hostingEnvironment, s.ControllerName, s.ControllerType, routes, "", "", UrlParameter.Optional, "api", - isMvc: false, - areaPathPrefix: s.IsBackOffice ? "backoffice" : null); - } - } - } -} diff --git a/src/Umbraco.Web/Runtime/WebInitialComponent.cs b/src/Umbraco.Web/Runtime/WebInitialComponent.cs index c11d648b37..5abcabfd6e 100644 --- a/src/Umbraco.Web/Runtime/WebInitialComponent.cs +++ b/src/Umbraco.Web/Runtime/WebInitialComponent.cs @@ -116,82 +116,8 @@ namespace Umbraco.Web.Runtime umbracoPath + "/RenderMvc/{action}/{id}", new { controller = "RenderMvc", action = "Index", id = UrlParameter.Optional } ); + defaultRoute.RouteHandler = new RenderRouteHandler(umbracoContextAccessor, ControllerBuilder.Current.GetControllerFactory(), shortStringHelper); - - // register install routes - // RouteTable.Routes.RegisterArea(); - - // register all back office routes - // RouteTable.Routes.RegisterArea(new BackOfficeArea(globalSettings, hostingEnvironment)); - - // plugin controllers must come first because the next route will catch many things - RoutePluginControllers(globalSettings, apiControllerTypes, hostingEnvironment); - } - - private static void RoutePluginControllers( - GlobalSettings globalSettings, - UmbracoApiControllerTypeCollection apiControllerTypes, - IHostingEnvironment hostingEnvironment) - { - var umbracoPath = globalSettings.GetUmbracoMvcArea(hostingEnvironment); - - // need to find the plugin controllers and route them - var pluginControllers = apiControllerTypes; //TODO was: surfaceControllerTypes.Concat(apiControllerTypes).ToArray(); - - // local controllers do not contain the attribute - var localControllers = pluginControllers.Where(x => PluginController.GetMetadata(x).AreaName.IsNullOrWhiteSpace()); - foreach (var s in localControllers) - { - if (TypeHelper.IsTypeAssignableFrom(s)) - RouteLocalSurfaceController(s, umbracoPath); - else if (TypeHelper.IsTypeAssignableFrom(s)) - RouteLocalApiController(s, umbracoPath); - } - - // get the plugin controllers that are unique to each area (group by) - var pluginSurfaceControlleres = pluginControllers.Where(x => PluginController.GetMetadata(x).AreaName.IsNullOrWhiteSpace() == false); - var groupedAreas = pluginSurfaceControlleres.GroupBy(controller => PluginController.GetMetadata(controller).AreaName); - // loop through each area defined amongst the controllers - foreach (var g in groupedAreas) - { - // create & register an area for the controllers (this will throw an exception if all controllers are not in the same area) - var pluginControllerArea = new PluginControllerArea(globalSettings, hostingEnvironment, g.Select(PluginController.GetMetadata)); - RouteTable.Routes.RegisterArea(pluginControllerArea); - } - } - - private static void RouteLocalApiController(Type controller, string umbracoPath) - { - var meta = PluginController.GetMetadata(controller); - var url = umbracoPath + (meta.IsBackOffice ? "/BackOffice" : "") + "/Api/" + meta.ControllerName + "/{action}/{id}"; - var route = RouteTable.Routes.MapHttpRoute( - $"umbraco-api-{meta.ControllerName}", - url, // URL to match - new { controller = meta.ControllerName, id = UrlParameter.Optional }, - new[] { meta.ControllerNamespace }); - if (route.DataTokens == null) // web api routes don't set the data tokens object - route.DataTokens = new RouteValueDictionary(); - - // TODO: Pretty sure this is not necessary, we'll see - //route.DataTokens.Add(Core.Constants.Web.UmbracoDataToken, "api"); //ensure the umbraco token is set - } - - private static void RouteLocalSurfaceController(Type controller, string umbracoPath) - { - var meta = PluginController.GetMetadata(controller); - var url = umbracoPath + "/Surface/" + meta.ControllerName + "/{action}/{id}"; - var route = RouteTable.Routes.MapRoute( - $"umbraco-surface-{meta.ControllerName}", - url, // URL to match - new { controller = meta.ControllerName, action = "Index", id = UrlParameter.Optional }, - new[] { meta.ControllerNamespace }); // look in this namespace to create the controller - - // TODO: Pretty sure this is not necessary, we'll see - //route.DataTokens.Add(Core.Constants.Web.UmbracoDataToken, "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(); } } } diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 2113175e51..b8829a557d 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -175,7 +175,6 @@ - @@ -224,7 +223,6 @@ Strings.resx -