diff --git a/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs b/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs deleted file mode 100644 index 7edb316c2e..0000000000 --- a/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs +++ /dev/null @@ -1,202 +0,0 @@ -using System; -using System.Linq; -using System.Web.Mvc; -using System.Web.Routing; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; -using Microsoft.Extensions.Logging; -using Moq; -using NUnit.Framework; -using Umbraco.Core; -using Umbraco.Core.Cache; -using Umbraco.Core.Configuration.Models; -using Umbraco.Core.Logging; -using Umbraco.Core.Models; -using Umbraco.Core.Models.PublishedContent; -using Umbraco.Core.Services; -using Umbraco.Core.Strings; -using Umbraco.Tests.Common; -using Umbraco.Tests.PublishedContent; -using Umbraco.Tests.TestHelpers; -using Umbraco.Tests.TestHelpers.Stubs; -using Umbraco.Tests.Testing; -using Umbraco.Web; -using Umbraco.Web.Models; -using Umbraco.Web.Mvc; -using Umbraco.Web.Runtime; -using Umbraco.Web.WebApi; -using Current = Umbraco.Web.Composing.Current; -using Umbraco.Core.DependencyInjection; -using System.Threading.Tasks; - -namespace Umbraco.Tests.Routing -{ - [TestFixture] - [UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerFixture)] - public class RenderRouteHandlerTests : BaseWebTest - { - public override void SetUp() - { - base.SetUp(); - - WebInitialComponent.CreateRoutes( - new TestUmbracoContextAccessor(), - TestObjects.GetGlobalSettings(), - ShortStringHelper, - // new SurfaceControllerTypeCollection(Enumerable.Empty()), - new UmbracoApiControllerTypeCollection(Enumerable.Empty()), - HostingEnvironment); - } - - protected override void Compose() - { - base.Compose(); - - // set the default RenderMvcController - Current.DefaultRenderMvcControllerType = typeof(RenderMvcController); // FIXME: Wrong! - - // var surfaceControllerTypes = new SurfaceControllerTypeCollection(Composition.TypeLoader.GetSurfaceControllers()); - // Composition.Services.AddUnique(surfaceControllerTypes); - - var umbracoApiControllerTypes = new UmbracoApiControllerTypeCollection(Builder.TypeLoader.GetUmbracoApiControllers()); - Builder.Services.AddUnique(umbracoApiControllerTypes); - - var requestHandlerSettings = new RequestHandlerSettings(); - Builder.Services.AddUnique(_ => new DefaultShortStringHelper(Microsoft.Extensions.Options.Options.Create(requestHandlerSettings))); - } - - public override void TearDown() - { - base.TearDown(); - RouteTable.Routes.Clear(); - } - - Template CreateTemplate(string alias) - { - var name = "Template"; - var template = new Template(ShortStringHelper, name, alias); - template.Content = ""; // else saving throws with a dirty internal error - ServiceContext.FileService.SaveTemplate(template); - return template; - } - - /// - /// Will route to the default controller and action since no custom controller is defined for this node route - /// - [Test] - public async Task Umbraco_Route_Umbraco_Defined_Controller_Action() - { - var url = "~/dummy-page"; - var template = CreateTemplate("homePage"); - var route = RouteTable.Routes["Umbraco_default"]; - var routeData = new RouteData { Route = route }; - var umbracoContext = GetUmbracoContext(url, template.Id, routeData); - var httpContext = GetHttpContextFactory(url, routeData).HttpContext; - var publishedRouter = CreatePublishedRouter(GetUmbracoContextAccessor(umbracoContext)); - var frequest = await publishedRouter .CreateRequestAsync(umbracoContext.CleanedUmbracoUrl); - frequest.SetPublishedContent(umbracoContext.Content.GetById(1174)); - frequest.SetTemplate(template); - - var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); - var handler = new RenderRouteHandler(umbracoContext, new TestControllerFactory(umbracoContextAccessor, Mock.Of>()), ShortStringHelper); - - handler.GetHandlerForRoute(httpContext.Request.RequestContext, frequest.Build()); - Assert.AreEqual("RenderMvc", routeData.Values["controller"].ToString()); - //the route action will still be the one we've asked for because our RenderActionInvoker is the thing that decides - // if the action matches. - Assert.AreEqual("homePage", routeData.Values["action"].ToString()); - } - - //test all template name styles to match the ActionName - - //[TestCase("home-\\234^^*32page")] // TODO: This fails! - [TestCase("home-page")] - [TestCase("home-page")] - [TestCase("home-page")] - [TestCase("Home-Page")] - [TestCase("HomePage")] - [TestCase("homePage")] - [TestCase("site1/template2")] - [TestCase("site1\\template2")] - public async Task Umbraco_Route_User_Defined_Controller_Action(string templateName) - { - // NOTE - here we create templates with crazy aliases... assuming that these - // could exist in the database... yet creating templates should sanitize - // aliases one way or another... - - var url = "~/dummy-page"; - var template = CreateTemplate(templateName); - var route = RouteTable.Routes["Umbraco_default"]; - var routeData = new RouteData() { Route = route }; - var umbracoContext = GetUmbracoContext("~/dummy-page", template.Id, routeData, true); - var httpContext = GetHttpContextFactory(url, routeData).HttpContext; - var publishedRouter = CreatePublishedRouter(GetUmbracoContextAccessor(umbracoContext)); - var frequest = await publishedRouter .CreateRequestAsync(umbracoContext.CleanedUmbracoUrl); - frequest.SetPublishedContent(umbracoContext.Content.GetById(1172)); - frequest.SetTemplate(template); - - var umbracoContextAccessor = new TestUmbracoContextAccessor(umbracoContext); - var type = new AutoPublishedContentType(Guid.NewGuid(), 22, "CustomDocument", new PublishedPropertyType[] { }); - ContentTypesCache.GetPublishedContentTypeByAlias = alias => type; - - var handler = new RenderRouteHandler(umbracoContext, new TestControllerFactory(umbracoContextAccessor, Mock.Of>(), context => - { - - return new CustomDocumentController(Factory.GetRequiredService>(), - umbracoContextAccessor, - Factory.GetRequiredService(), - Factory.GetRequiredService(), - Factory.GetRequiredService(), - Factory.GetRequiredService()); - }), ShortStringHelper); - - handler.GetHandlerForRoute(httpContext.Request.RequestContext, frequest.Build()); - Assert.AreEqual("CustomDocument", routeData.Values["controller"].ToString()); - Assert.AreEqual( - //global::umbraco.cms.helpers.Casing.SafeAlias(template.Alias), - template.Alias.ToSafeAlias(ShortStringHelper), - routeData.Values["action"].ToString()); - } - - - #region Internal classes - - ///// - ///// Used to test a user route (non-umbraco) - ///// - //private class CustomUserController : Controller - //{ - - // public ActionResult Index() - // { - // return View(); - // } - - // public ActionResult Test(int id) - // { - // return View(); - // } - - //} - - /// - /// Used to test a user route umbraco route - /// - public class CustomDocumentController : RenderMvcController - { - public CustomDocumentController(IOptions globalSettings, IUmbracoContextAccessor umbracoContextAccessor, ServiceContext services, AppCaches appCaches, IProfilingLogger profilingLogger, ILoggerFactory loggerFactory) - - { - } - - public ActionResult HomePage(ContentModel model) - { - // ReSharper disable once Mvc.ViewNotResolved - return View(); - } - - } - - #endregion - } -} diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 65c1b59528..730e2e80b1 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -220,7 +220,6 @@ - diff --git a/src/Umbraco.Web/Composing/Current.cs b/src/Umbraco.Web/Composing/Current.cs index 8f698db995..5b2c1a8733 100644 --- a/src/Umbraco.Web/Composing/Current.cs +++ b/src/Umbraco.Web/Composing/Current.cs @@ -1,4 +1,4 @@ -using System; +using System; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Umbraco.Core; @@ -151,28 +151,7 @@ namespace Umbraco.Web.Composing #endregion - #region Web Constants - - // these are different - not 'resolving' anything, and nothing that could be managed - // by the container - just registering some sort of application-wide constants or - // settings - but they fit in Current nicely too - - private static Type _defaultRenderMvcControllerType; - - // internal - can only be accessed through Composition at compose time - internal static Type DefaultRenderMvcControllerType - { - get => _defaultRenderMvcControllerType; - set - { - if (value.Implements() == false) - throw new ArgumentException($"Type {value.FullName} does not implement {typeof(IRenderController).FullName}.", nameof(value)); - _defaultRenderMvcControllerType = value; - } - } - - #endregion - + #region Core Getters // proxy Core for convenience diff --git a/src/Umbraco.Web/Mvc/AreaRegistrationExtensions.cs b/src/Umbraco.Web/Mvc/AreaRegistrationExtensions.cs deleted file mode 100644 index c59c701d42..0000000000 --- a/src/Umbraco.Web/Mvc/AreaRegistrationExtensions.cs +++ /dev/null @@ -1,137 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Web.Http; -using System.Web.Mvc; -using System.Web.Routing; -using System.Web.SessionState; -using Umbraco.Core; -using Umbraco.Core.Configuration; -using Umbraco.Core.Configuration.Models; -using Umbraco.Core.Hosting; -using Umbraco.Web.WebApi; - -namespace Umbraco.Web.Mvc -{ - internal static class AreaRegistrationExtensions - { - /// - /// Creates a custom individual route for the specified controller plugin. Individual routes - /// are required by controller plugins to map to a unique URL based on ID. - /// - /// - /// - /// - /// An existing route collection - /// - /// The suffix name that the controller name must end in before the "Controller" string for example: - /// ContentTreeController has a controllerSuffixName of "Tree", this is used for route constraints. - /// - /// - /// - /// - /// The DataToken value to set for the 'umbraco' key, this defaults to 'backoffice' - /// By default this value is just {action}/{id} but can be modified for things like web api routes - /// Default is true for MVC, otherwise false for WebAPI - /// - /// If specified will add this string to the path between the umbraco path and the area path name, for example: - /// /umbraco/CUSTOMPATHPREFIX/areaname - /// if not specified, will just route like: - /// /umbraco/areaname - /// - /// - /// - /// - internal static Route RouteControllerPlugin(this AreaRegistration area, - GlobalSettings globalSettings, IHostingEnvironment hostingEnvironment, - string controllerName, Type controllerType, RouteCollection routes, - string controllerSuffixName, string defaultAction, object defaultId, - string umbracoTokenValue = "backoffice", - string routeTokens = "{action}/{id}", - bool isMvc = true, - string areaPathPrefix = "") - { - if (controllerName == null) throw new ArgumentNullException(nameof(controllerName)); - if (string.IsNullOrEmpty(controllerName)) throw new ArgumentException("Value can't be empty.", nameof(controllerName)); - if (controllerSuffixName == null) throw new ArgumentNullException(nameof(controllerSuffixName)); - - if (controllerType == null) throw new ArgumentNullException(nameof(controllerType)); - if (routes == null) throw new ArgumentNullException(nameof(routes)); - if (defaultId == null) throw new ArgumentNullException(nameof(defaultId)); - - var umbracoArea = globalSettings.GetUmbracoMvcArea(hostingEnvironment); - - //routes are explicitly named with controller names and IDs - var url = umbracoArea + "/" + - (areaPathPrefix.IsNullOrWhiteSpace() ? "" : areaPathPrefix + "/") + - area.AreaName + "/" + controllerName + "/" + routeTokens; - - Route controllerPluginRoute; - //var meta = PluginController.GetMetadata(controllerType); - if (isMvc) - { - //create a new route with custom name, specified URL, and the namespace of the controller plugin - controllerPluginRoute = routes.MapRoute( - //name - string.Format("umbraco-{0}-{1}", area.AreaName, controllerName), - //URL format - url, - //set the namespace of the controller to match - new[] {controllerType.Namespace}); - - //set defaults - controllerPluginRoute.Defaults = new RouteValueDictionary( - new Dictionary - { - {"controller", controllerName}, - {"action", defaultAction}, - {"id", defaultId} - }); - } - else - { - controllerPluginRoute = routes.MapHttpRoute( - //name - string.Format("umbraco-{0}-{1}-{2}", "api", area.AreaName, controllerName), - //URL format - url, - new {controller = controllerName, id = defaultId}); - //web api routes don't set the data tokens object - if (controllerPluginRoute.DataTokens == null) - { - controllerPluginRoute.DataTokens = new RouteValueDictionary(); - } - - //look in this namespace to create the controller - controllerPluginRoute.DataTokens.Add("Namespaces", new[] {controllerType.Namespace}); - - //Special case! Check if the controller type implements IRequiresSessionState and if so use our - //custom webapi session handler - if (typeof(IRequiresSessionState).IsAssignableFrom(controllerType)) - { - controllerPluginRoute.RouteHandler = new SessionHttpControllerRouteHandler(); - } - } - - //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 - if (controllerSuffixName.IsNullOrWhiteSpace() == false) - { - controllerPluginRoute.Constraints = new RouteValueDictionary( - new Dictionary - { - {"controller", @"(\w+)" + controllerSuffixName} - }); - } - - //match this area - controllerPluginRoute.DataTokens.Add("area", area.AreaName); - - // TODO: No longer needed, remove - //controllerPluginRoute.DataTokens.Add(Core.Constants.Web.UmbracoDataToken, umbracoTokenValue); //ensure the umbraco token is set - - return controllerPluginRoute; - } - } -} diff --git a/src/Umbraco.Web/Mvc/ControllerExtensions.cs b/src/Umbraco.Web/Mvc/ControllerExtensions.cs index d7a693be2d..c72a7554f6 100644 --- a/src/Umbraco.Web/Mvc/ControllerExtensions.cs +++ b/src/Umbraco.Web/Mvc/ControllerExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Threading; using System.Web.Mvc; @@ -43,164 +43,5 @@ namespace Umbraco.Web.Mvc return GetControllerName(typeof(T)); } - /// - /// This is generally used for proxying to a ChildAction which requires a ViewContext to be setup - /// but since the View isn't actually rendered the IView object is null, however the rest of the - /// properties are filled in. - /// - /// - /// - internal static ViewContext CreateEmptyViewContext(this ControllerBase controller) - { - return new ViewContext - { - Controller = controller, - HttpContext = controller.ControllerContext.HttpContext, - RequestContext = controller.ControllerContext.RequestContext, - RouteData = controller.ControllerContext.RouteData, - TempData = controller.TempData, - ViewData = controller.ViewData - }; - } - - /// - /// Returns the string output from a ViewResultBase object - /// - /// - /// - /// - internal static string RenderViewResultAsString(this ControllerBase controller, ViewResultBase viewResult) - { - using (var sw = new StringWriter()) - { - controller.EnsureViewObjectDataOnResult(viewResult); - - var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, viewResult.ViewData, viewResult.TempData, sw); - viewResult.View.Render(viewContext, sw); - foreach (var v in viewResult.ViewEngineCollection) - { - v.ReleaseView(controller.ControllerContext, viewResult.View); - } - return sw.ToString().Trim(); - } - } - - /// - /// Renders the partial view to string. - /// - /// The controller context. - /// Name of the view. - /// The model. - /// true if it is a Partial view, otherwise false for a normal view - /// - internal static string RenderViewToString(this ControllerBase controller, string viewName, object model, bool isPartial = false) - { - if (controller.ControllerContext == null) - throw new ArgumentException("The controller must have an assigned ControllerContext to execute this method."); - - controller.ViewData.Model = model; - - using (var sw = new StringWriter()) - { - var viewResult = isPartial == false - ? ViewEngines.Engines.FindView(controller.ControllerContext, viewName, null) - : ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName); - if (viewResult.View == null) - throw new InvalidOperationException("No view could be found by name " + viewName); - var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw); - viewResult.View.Render(viewContext, sw); - viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View); - return sw.GetStringBuilder().ToString(); - } - } - - /// - /// Renders the partial view to string. - /// - /// The request context. - /// - /// - /// Name of the view. - /// The model. - /// true if it is a Partial view, otherwise false for a normal view - /// - internal static string RenderViewToString( - this RequestContext requestContext, - ViewDataDictionary viewData, - TempDataDictionary tempData, - string viewName, object model, bool isPartial = false) - { - if (requestContext == null) throw new ArgumentNullException("requestContext"); - if (viewData == null) throw new ArgumentNullException("viewData"); - if (tempData == null) throw new ArgumentNullException("tempData"); - - var routeData = requestContext.RouteData; - if (routeData.Values.ContainsKey("controller") == false) - routeData.Values.Add("controller", "Fake"); - viewData.Model = model; - var controllerContext = new ControllerContext( - requestContext.HttpContext, routeData, - new FakeController - { - ViewData = viewData - }); - - using (var sw = new StringWriter()) - { - var viewResult = isPartial == false - ? ViewEngines.Engines.FindView(controllerContext, viewName, null) - : ViewEngines.Engines.FindPartialView(controllerContext, viewName); - if (viewResult.View == null) - throw new InvalidOperationException("No view could be found by name " + viewName); - var viewContext = new ViewContext(controllerContext, viewResult.View, viewData, tempData, sw); - viewResult.View.Render(viewContext, sw); - viewResult.ViewEngine.ReleaseView(controllerContext, viewResult.View); - return sw.GetStringBuilder().ToString(); - } - } - - private class FakeController : ControllerBase { protected override void ExecuteCore() { } } - - /// - /// Normally in MVC the way that the View object gets assigned to the result is to Execute the ViewResult, this however - /// will write to the Response output stream which isn't what we want. Instead, this method will use the same logic inside - /// of MVC to assign the View object to the result but without executing it. - /// This is only relevant for view results of PartialViewResult or ViewResult. - /// - /// - /// - private static void EnsureViewObjectDataOnResult(this ControllerBase controller, ViewResultBase result) - { - if (result.View != null) return; - - if (string.IsNullOrEmpty(result.ViewName)) - result.ViewName = controller.ControllerContext.RouteData.GetRequiredString("action"); - - if (result.View != null) return; - - if (result is PartialViewResult) - { - var viewEngineResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, result.ViewName); - - if (viewEngineResult.View == null) - { - throw new InvalidOperationException("Could not find the view " + result.ViewName + ", the following locations were searched: " + Environment.NewLine + string.Join(Environment.NewLine, viewEngineResult.SearchedLocations)); - } - - result.View = viewEngineResult.View; - } - else if (result is ViewResult) - { - var vr = (ViewResult)result; - var viewEngineResult = ViewEngines.Engines.FindView(controller.ControllerContext, vr.ViewName, vr.MasterName); - - if (viewEngineResult.View == null) - { - throw new InvalidOperationException("Could not find the view " + vr.ViewName + ", the following locations were searched: " + Environment.NewLine + string.Join(Environment.NewLine, viewEngineResult.SearchedLocations)); - } - - result.View = viewEngineResult.View; - } - } } } diff --git a/src/Umbraco.Web/Mvc/ControllerFactoryExtensions.cs b/src/Umbraco.Web/Mvc/ControllerFactoryExtensions.cs deleted file mode 100644 index a69b09efe0..0000000000 --- a/src/Umbraco.Web/Mvc/ControllerFactoryExtensions.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Web.Mvc; -using System.Web.Routing; - -namespace Umbraco.Web.Mvc -{ - internal static class ControllerFactoryExtensions - { - /// - /// Gets a controller type by the name - /// - /// - /// - /// - /// - /// - /// This is related to issue: http://issues.umbraco.org/issue/U4-1726. We already have a method called GetControllerTypeInternal on our MasterControllerFactory, - /// however, we cannot always guarantee that the usage of this will be a MasterControllerFactory like during unit tests. So we needed to create - /// this extension method to do the checks instead. - /// - internal static Type GetControllerTypeInternal(this IControllerFactory factory, RequestContext requestContext, string controllerName) - { - - //TODO Reintroduce for netcore - // if (factory is MasterControllerFactory controllerFactory) - // return controllerFactory.GetControllerTypeInternal(requestContext, controllerName); - - //we have no choice but to instantiate the controller - var instance = factory.CreateController(requestContext, controllerName); - var controllerType = instance?.GetType(); - factory.ReleaseController(instance); - - return controllerType; - } - } -} diff --git a/src/Umbraco.Web/Mvc/HttpUmbracoFormRouteStringException.cs b/src/Umbraco.Web/Mvc/HttpUmbracoFormRouteStringException.cs deleted file mode 100644 index db2040665c..0000000000 --- a/src/Umbraco.Web/Mvc/HttpUmbracoFormRouteStringException.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Runtime.Serialization; -using System.Web; - -namespace Umbraco.Web.Mvc -{ - /// - /// Exception that occurs when an Umbraco form route string is invalid - /// - /// - [Serializable] - public sealed class HttpUmbracoFormRouteStringException : HttpException - { - /// - /// Initializes a new instance of the class. - /// - public HttpUmbracoFormRouteStringException() - { } - - /// - /// Initializes a new instance of the class. - /// - /// The that holds the serialized object data about the exception being thrown. - /// The that holds the contextual information about the source or destination. - private HttpUmbracoFormRouteStringException(SerializationInfo info, StreamingContext context) - : base(info, context) - { } - - /// - /// Initializes a new instance of the class. - /// - /// The error message displayed to the client when the exception is thrown. - public HttpUmbracoFormRouteStringException(string message) - : base(message) - { } - - /// - /// Initializes a new instance of the class. - /// - /// The error message displayed to the client when the exception is thrown. - /// The , if any, that threw the current exception. - public HttpUmbracoFormRouteStringException(string message, Exception innerException) - : base(message, innerException) - { } - } -} diff --git a/src/Umbraco.Web/Mvc/IRenderController.cs b/src/Umbraco.Web/Mvc/IRenderController.cs deleted file mode 100644 index cddd51ef5f..0000000000 --- a/src/Umbraco.Web/Mvc/IRenderController.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Umbraco.Web.Mvc -{ - //Migrated to .NET Core - public interface IRenderController - { - - } -} diff --git a/src/Umbraco.Web/Mvc/IRenderMvcController.cs b/src/Umbraco.Web/Mvc/IRenderMvcController.cs deleted file mode 100644 index 542e46ac2c..0000000000 --- a/src/Umbraco.Web/Mvc/IRenderMvcController.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Umbraco.Web.Mvc -{ - //Migrated to .NET Core - public interface IRenderMvcController : IRenderController - { - - } -} diff --git a/src/Umbraco.Web/Mvc/NotChildAction.cs b/src/Umbraco.Web/Mvc/NotChildAction.cs deleted file mode 100644 index fc8ca7c1ae..0000000000 --- a/src/Umbraco.Web/Mvc/NotChildAction.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Web.Mvc; - -namespace Umbraco.Web.Mvc -{ - /// - /// Used to ensure that actions with duplicate names that are not child actions don't get executed when - /// we are Posting and not redirecting. - /// - /// - /// See issue: http://issues.umbraco.org/issue/U4-1819 - /// - public class NotChildAction : ActionMethodSelectorAttribute - { - public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo) - { - var isChildAction = controllerContext.IsChildAction; - return !isChildAction; - } - } -} diff --git a/src/Umbraco.Web/Mvc/NotFoundHandler.cs b/src/Umbraco.Web/Mvc/NotFoundHandler.cs deleted file mode 100644 index bb98b42028..0000000000 --- a/src/Umbraco.Web/Mvc/NotFoundHandler.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Web; - -namespace Umbraco.Web.Mvc -{ - public class NotFoundHandler : IHttpHandler - { - public void ProcessRequest(HttpContext context) - { - context.Response.StatusCode = 404; - } - - public bool IsReusable - { - get { return true; } - } - } -} diff --git a/src/Umbraco.Web/Mvc/PostedDataProxyInfo.cs b/src/Umbraco.Web/Mvc/PostedDataProxyInfo.cs deleted file mode 100644 index 2af2e0fbd0..0000000000 --- a/src/Umbraco.Web/Mvc/PostedDataProxyInfo.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace Umbraco.Web.Mvc -{ - /// - /// Represents the data required to proxy a request to a surface controller for posted data - /// - internal class PostedDataProxyInfo : RouteDefinition - { - public string Area { get; set; } - } -} diff --git a/src/Umbraco.Web/Mvc/ProfilingView.cs b/src/Umbraco.Web/Mvc/ProfilingView.cs deleted file mode 100644 index ce733552d6..0000000000 --- a/src/Umbraco.Web/Mvc/ProfilingView.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System.IO; -using System.Web.Mvc; -using Umbraco.Web.Composing; - -namespace Umbraco.Web.Mvc -{ - public class ProfilingView : IView - { - private readonly IView _inner; - private readonly string _name; - private readonly string _viewPath; - - public ProfilingView(IView inner) - { - _inner = inner; - _name = inner.GetType().Name; - var razorView = inner as RazorView; - _viewPath = razorView != null ? razorView.ViewPath : "Unknown"; - } - - public void Render(ViewContext viewContext, TextWriter writer) - { - using (Current.Profiler.Step(string.Format("{0}.Render: {1}", _name, _viewPath))) - { - _inner.Render(viewContext, writer); - } - } - } -} diff --git a/src/Umbraco.Web/Mvc/QueryStringFilterAttribute.cs b/src/Umbraco.Web/Mvc/QueryStringFilterAttribute.cs deleted file mode 100644 index 6551eb99f3..0000000000 --- a/src/Umbraco.Web/Mvc/QueryStringFilterAttribute.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web.Mvc; -using Umbraco.Core; - -namespace Umbraco.Web.Mvc -{ - - /// - /// Allows an Action to execute with an arbitrary number of QueryStrings - /// - /// - /// Just like you can POST an arbitrary number of parameters to an Action, you can't GET an arbitrary number - /// but this will allow you to do it - /// - /// http://stackoverflow.com/questions/488061/passing-multiple-parameters-to-controller-in-asp-net-mvc-also-generating-on-the - /// - public class QueryStringFilterAttribute : ActionFilterAttribute - { - public string ParameterName { get; private set; } - - public QueryStringFilterAttribute(string parameterName) - { - if (string.IsNullOrEmpty(parameterName)) - throw new ArgumentException("ParameterName is required."); - ParameterName = parameterName; - } - - public override void OnActionExecuting(ActionExecutingContext filterContext) - { - var nonNullKeys = filterContext.HttpContext.Request.QueryString.AllKeys.Where(x => !string.IsNullOrEmpty(x)); - var vals = nonNullKeys.ToDictionary(q => q, q => (object)filterContext.HttpContext.Request.QueryString[q]); - var qs = ToFormCollection(vals); - - filterContext.ActionParameters[ParameterName] = qs; - - base.OnActionExecuting(filterContext); - } - - /// - /// Converts a dictionary to a FormCollection - /// - /// - /// - public static FormCollection ToFormCollection(IDictionary d) - { - var n = new FormCollection(); - foreach (var i in d) - { - n.Add(i.Key, Convert.ToString(i.Value)); - } - return n; - } - - } - -} diff --git a/src/Umbraco.Web/Mvc/RenderActionInvoker.cs b/src/Umbraco.Web/Mvc/RenderActionInvoker.cs deleted file mode 100644 index 47e77bb4f4..0000000000 --- a/src/Umbraco.Web/Mvc/RenderActionInvoker.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using System.Web.Mvc; -using System.Web.Mvc.Async; - -namespace Umbraco.Web.Mvc -{ - /// - /// Ensures that if an action for the Template name is not explicitly defined by a user, that the 'Index' action will execute - /// - public class RenderActionInvoker : AsyncControllerActionInvoker - { - /// - /// Ensures that if an action for the Template name is not explicitly defined by a user, that the 'Index' action will execute - /// - /// - /// - /// - /// - protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName) - { - var ad = base.FindAction(controllerContext, controllerDescriptor, actionName); - - //now we need to check if it exists, if not we need to return the Index by default - if (ad == null) - { - //check if the controller is an instance of IRenderController and find the index - if (controllerContext.Controller is IRenderController) - { - // ReSharper disable once Mvc.ActionNotResolved - return controllerDescriptor.FindAction(controllerContext, "Index"); - } - } - return ad; - } - - } -} diff --git a/src/Umbraco.Web/Mvc/RenderMvcController.cs b/src/Umbraco.Web/Mvc/RenderMvcController.cs deleted file mode 100644 index 1a4a32eadf..0000000000 --- a/src/Umbraco.Web/Mvc/RenderMvcController.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Web.Mvc; - -namespace Umbraco.Web.Mvc -{ - //Migrated to .NET Core - public class RenderMvcController : Controller, IRenderMvcController - { - - - } -} diff --git a/src/Umbraco.Web/Mvc/RenderRouteHandler.cs b/src/Umbraco.Web/Mvc/RenderRouteHandler.cs index f5b536d259..2cd491b7a7 100644 --- a/src/Umbraco.Web/Mvc/RenderRouteHandler.cs +++ b/src/Umbraco.Web/Mvc/RenderRouteHandler.cs @@ -13,7 +13,8 @@ using Current = Umbraco.Web.Composing.Current; namespace Umbraco.Web.Mvc { - public class RenderRouteHandler : IRouteHandler + // NOTE: already migrated to netcore, just here since the below is referenced still + public class RenderRouteHandler { // Define reserved dictionary keys for controller, action and area specified in route additional values data internal static class ReservedAdditionalKeys @@ -22,186 +23,5 @@ namespace Umbraco.Web.Mvc internal const string Action = "a"; internal const string Area = "ar"; } - - private readonly IUmbracoContextAccessor _umbracoContextAccessor; - private readonly IUmbracoContext _umbracoContext; - - public RenderRouteHandler(IUmbracoContextAccessor umbracoContextAccessor, IControllerFactory controllerFactory, IShortStringHelper shortStringHelper) - { - _umbracoContextAccessor = umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor)); - } - - public RenderRouteHandler(IUmbracoContext umbracoContext, IControllerFactory controllerFactory, IShortStringHelper shortStringHelper) - { - _umbracoContext = umbracoContext ?? throw new ArgumentNullException(nameof(umbracoContext)); - } - - private IUmbracoContext UmbracoContext => _umbracoContext ?? _umbracoContextAccessor.UmbracoContext; - - #region IRouteHandler Members - - /// - /// Assigns the correct controller based on the Umbraco request and returns a standard MvcHandler to process the response, - /// this also stores the render model into the data tokens for the current RouteData. - /// - public IHttpHandler GetHttpHandler(RequestContext requestContext) - { - if (UmbracoContext == null) - { - throw new NullReferenceException("There is no current UmbracoContext, it must be initialized before the RenderRouteHandler executes"); - } - var request = UmbracoContext.PublishedRequest; - if (request == null) - { - throw new NullReferenceException("There is no current PublishedRequest, it must be initialized before the RenderRouteHandler executes"); - } - - return GetHandlerForRoute(requestContext, request); - - } - - #endregion - - /// - /// Checks the request and query strings to see if it matches the definition of having a Surface controller - /// posted/get value, if so, then we return a PostedDataProxyInfo object with the correct information. - /// - internal static PostedDataProxyInfo GetFormInfo(RequestContext requestContext) - { - if (requestContext == null) throw new ArgumentNullException(nameof(requestContext)); - - // if it is a POST/GET then a value must be in the request - if (requestContext.HttpContext.Request.QueryString["ufprt"].IsNullOrWhiteSpace() - && requestContext.HttpContext.Request.Form["ufprt"].IsNullOrWhiteSpace()) - { - return null; - } - - string encodedVal; - - switch (requestContext.HttpContext.Request.RequestType) - { - case "POST": - // get the value from the request. - // this field will contain an encrypted version of the surface route vals. - encodedVal = requestContext.HttpContext.Request.Form["ufprt"]; - break; - case "GET": - // this field will contain an encrypted version of the surface route vals. - encodedVal = requestContext.HttpContext.Request.QueryString["ufprt"]; - break; - default: - return null; - } - - if (!UmbracoHelper.DecryptAndValidateEncryptedRouteString(encodedVal, out var decodedParts)) - return null; - - foreach (var item in decodedParts.Where(x => new[] { - ReservedAdditionalKeys.Controller, - ReservedAdditionalKeys.Action, - ReservedAdditionalKeys.Area }.Contains(x.Key) == false)) - { - // Populate route with additional values which aren't reserved values so they eventually to action parameters - requestContext.RouteData.Values[item.Key] = item.Value; - } - - //return the proxy info without the surface id... could be a local controller. - return new PostedDataProxyInfo - { - ControllerName = HttpUtility.UrlDecode(decodedParts.Single(x => x.Key == ReservedAdditionalKeys.Controller).Value), - ActionName = HttpUtility.UrlDecode(decodedParts.Single(x => x.Key == ReservedAdditionalKeys.Action).Value), - Area = HttpUtility.UrlDecode(decodedParts.Single(x => x.Key == ReservedAdditionalKeys.Area).Value), - }; - } - - - - /// - /// Handles a posted form to an Umbraco URL and ensures the correct controller is routed to and that - /// the right DataTokens are set. - /// - internal static IHttpHandler HandlePostedValues(RequestContext requestContext, PostedDataProxyInfo postedInfo) - { - if (requestContext == null) throw new ArgumentNullException(nameof(requestContext)); - if (postedInfo == null) throw new ArgumentNullException(nameof(postedInfo)); - - //set the standard route values/tokens - requestContext.RouteData.Values["controller"] = postedInfo.ControllerName; - requestContext.RouteData.Values["action"] = postedInfo.ActionName; - - IHttpHandler handler; - - //get the route from the defined routes - using (RouteTable.Routes.GetReadLock()) - { - Route surfaceRoute; - - //find the controller in the route table - var surfaceRoutes = RouteTable.Routes.OfType() - .Where(x => x.Defaults != null && - x.Defaults.ContainsKey("controller") && - x.Defaults["controller"].ToString().InvariantEquals(postedInfo.ControllerName) && - // Only return surface controllers - x.DataTokens["umbraco"].ToString().InvariantEquals("surface") && - // Check for area token if the area is supplied - (postedInfo.Area.IsNullOrWhiteSpace() ? !x.DataTokens.ContainsKey("area") : x.DataTokens["area"].ToString().InvariantEquals(postedInfo.Area))) - .ToList(); - - // If more than one route is found, find one with a matching action - if (surfaceRoutes.Count > 1) - { - surfaceRoute = surfaceRoutes.FirstOrDefault(x => - x.Defaults["action"] != null && - x.Defaults["action"].ToString().InvariantEquals(postedInfo.ActionName)); - } - else - { - surfaceRoute = surfaceRoutes.FirstOrDefault(); - } - - if (surfaceRoute == null) - throw new InvalidOperationException("Could not find a Surface controller route in the RouteTable for controller name " + postedInfo.ControllerName); - - //set the area if one is there. - if (surfaceRoute.DataTokens.ContainsKey("area")) - { - requestContext.RouteData.DataTokens["area"] = surfaceRoute.DataTokens["area"]; - } - - //set the 'Namespaces' token so the controller factory knows where to look to construct it - if (surfaceRoute.DataTokens.ContainsKey("Namespaces")) - { - requestContext.RouteData.DataTokens["Namespaces"] = surfaceRoute.DataTokens["Namespaces"]; - } - handler = surfaceRoute.RouteHandler.GetHttpHandler(requestContext); - - } - - return handler; - } - - /// - /// this will determine the controller and set the values in the route data - /// - internal IHttpHandler GetHandlerForRoute(RequestContext requestContext, IPublishedRequest request) - { - if (requestContext == null) throw new ArgumentNullException(nameof(requestContext)); - if (request == null) throw new ArgumentNullException(nameof(request)); - - //var routeDef = GetUmbracoRouteDefinition(requestContext, request); - - // TODO: Need to port this to netcore - // Need to check for a special case if there is form data being posted back to an Umbraco URL - var postedInfo = GetFormInfo(requestContext); - if (postedInfo != null) - { - return HandlePostedValues(requestContext, postedInfo); - } - - // NOTE: Code here has been removed and ported to netcore - throw new NotSupportedException("This code was already ported to netcore"); - } - } } diff --git a/src/Umbraco.Web/Mvc/RouteValueDictionaryExtensions.cs b/src/Umbraco.Web/Mvc/RouteValueDictionaryExtensions.cs deleted file mode 100644 index acfa4e5715..0000000000 --- a/src/Umbraco.Web/Mvc/RouteValueDictionaryExtensions.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.Linq; -using System.Web.Mvc; -using System.Web.Routing; - -namespace Umbraco.Web.Mvc -{ - internal static class RouteValueDictionaryExtensions - { - - /// - /// Converts a route value dictionary to a form collection - /// - /// - /// - public static FormCollection ToFormCollection(this RouteValueDictionary items) - { - var formCollection = new FormCollection(); - foreach (var i in items) - { - formCollection.Add(i.Key, i.Value != null ? i.Value.ToString() : null); - } - return formCollection; - } - - /// - /// Returns the value of a mandatory item in the route items - /// - /// - /// - /// - public static object GetRequiredObject(this RouteValueDictionary items, string key) - { - if (key == null) throw new ArgumentNullException("key"); - if (items.Keys.Contains(key) == false) - throw new ArgumentNullException("The " + key + " parameter was not found but is required"); - return items[key]; - } - - } -} diff --git a/src/Umbraco.Web/Mvc/SurfaceRouteHandler.cs b/src/Umbraco.Web/Mvc/SurfaceRouteHandler.cs deleted file mode 100644 index 065e51dfe0..0000000000 --- a/src/Umbraco.Web/Mvc/SurfaceRouteHandler.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Web; -using System.Web.Routing; - -namespace Umbraco.Web.Mvc -{ - /// - /// Assigned to all SurfaceController's so that it returns our custom SurfaceMvcHandler to use for rendering - /// - internal class SurfaceRouteHandler : IRouteHandler - { - public IHttpHandler GetHttpHandler(RequestContext requestContext) - { - return new UmbracoMvcHandler(requestContext); - } - } -} diff --git a/src/Umbraco.Web/Mvc/UmbracoAuthorizeAttribute.cs b/src/Umbraco.Web/Mvc/UmbracoAuthorizeAttribute.cs deleted file mode 100644 index 73b2674706..0000000000 --- a/src/Umbraco.Web/Mvc/UmbracoAuthorizeAttribute.cs +++ /dev/null @@ -1,109 +0,0 @@ -using System; -using System.Web; -using System.Web.Mvc; -using Umbraco.Core; -using Umbraco.Core.Configuration; -using Umbraco.Core.Security; -using Umbraco.Core.Configuration.Models; -using Umbraco.Web.Composing; -using Umbraco.Web.Security; - -namespace Umbraco.Web.Mvc -{ - // TODO: This has been migrated to netcore and can be removed when ready - - public sealed class UmbracoAuthorizeAttribute : AuthorizeAttribute - { - // see note in HttpInstallAuthorizeAttribute - private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; - private readonly IRuntimeState _runtimeState; - private readonly string _redirectUrl; - - private IRuntimeState RuntimeState => _runtimeState ?? Current.RuntimeState; - - private IBackOfficeSecurity BackOfficeSecurity => _backOfficeSecurityAccessor.BackOfficeSecurity ?? Current.BackOfficeSecurityAccessor.BackOfficeSecurity; - - /// - /// THIS SHOULD BE ONLY USED FOR UNIT TESTS - /// - /// - /// - public UmbracoAuthorizeAttribute(IBackOfficeSecurityAccessor backofficeSecurityAccessor, IRuntimeState runtimeState) - { - _backOfficeSecurityAccessor = backofficeSecurityAccessor ?? throw new ArgumentNullException(nameof(backofficeSecurityAccessor)); - _runtimeState = runtimeState ?? throw new ArgumentNullException(nameof(runtimeState)); - } - - /// - /// Default constructor - /// - public UmbracoAuthorizeAttribute() - { } - - /// - /// Constructor specifying to redirect to the specified location if not authorized - /// - /// - public UmbracoAuthorizeAttribute(string redirectUrl) - { - _redirectUrl = redirectUrl ?? throw new ArgumentNullException(nameof(redirectUrl)); - } - - /// - /// Constructor specifying to redirect to the umbraco login page if not authorized - /// - /// - public UmbracoAuthorizeAttribute(bool redirectToUmbracoLogin) - { - if (redirectToUmbracoLogin) - { - _redirectUrl = new GlobalSettings().GetBackOfficePath(Current.HostingEnvironment).EnsureStartsWith("~"); - } - } - - /// - /// Ensures that the user must be in the Administrator or the Install role - /// - /// - /// - protected override bool AuthorizeCore(HttpContextBase httpContext) - { - if (httpContext == null) throw new ArgumentNullException(nameof(httpContext)); - - try - { - // if not configured (install or upgrade) then we can continue - // otherwise we need to ensure that a user is logged in - return RuntimeState.Level == RuntimeLevel.Install - || RuntimeState.Level == RuntimeLevel.Upgrade - || (httpContext.User?.Identity?.IsAuthenticated ?? false); - } - catch (Exception) - { - return false; - } - } - - /// - /// Override to ensure no redirect occurs - /// - /// - protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) - { - if (_redirectUrl.IsNullOrWhiteSpace()) - { - filterContext.Result = (ActionResult)new HttpUnauthorizedResult("You must login to view this resource."); - - - } - else - { - filterContext.Result = new RedirectResult(_redirectUrl); - } - - // DON'T do a FormsAuth redirect... argh!! thankfully we're running .Net 4.5 :) - filterContext.RequestContext.HttpContext.Response.SuppressFormsAuthenticationRedirect = true; - } - - } -} diff --git a/src/Umbraco.Web/Mvc/UmbracoControllerFactory.cs b/src/Umbraco.Web/Mvc/UmbracoControllerFactory.cs deleted file mode 100644 index b4dde6c540..0000000000 --- a/src/Umbraco.Web/Mvc/UmbracoControllerFactory.cs +++ /dev/null @@ -1,88 +0,0 @@ -// using System; -// using System.Web.Mvc; -// using System.Web.Routing; -// using System.Web.SessionState; -// using Umbraco.Core; -// using Umbraco.Core.Composing; -// using Umbraco.Web.Composing; -// -// namespace Umbraco.Web.Mvc -// { -// /// -// /// Abstract filtered controller factory used for all Umbraco controller factory implementations -// /// -// public abstract class UmbracoControllerFactory : IFilteredControllerFactory -// { -// private readonly OverridenDefaultControllerFactory _innerFactory = new OverridenDefaultControllerFactory(); -// -// public abstract bool CanHandle(RequestContext request); -// -// public virtual Type GetControllerType(RequestContext requestContext, string controllerName) -// { -// return _innerFactory.GetControllerType(requestContext, controllerName); -// } -// -// /// -// /// Creates the specified controller by using the specified request context. -// /// -// /// -// /// The controller. -// /// -// /// The request context.The name of the controller. -// public virtual IController CreateController(RequestContext requestContext, string controllerName) -// { -// var controllerType = GetControllerType(requestContext, controllerName) ?? -// _innerFactory.GetControllerType( -// requestContext, -// ControllerExtensions.GetControllerName(Current.DefaultRenderMvcControllerType)); -// -// return _innerFactory.GetControllerInstance(requestContext, controllerType); -// } -// -// /// -// /// Gets the controller's session behavior. -// /// -// /// -// /// The controller's session behavior. -// /// -// /// The request context.The name of the controller whose session behavior you want to get. -// public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName) -// { -// return ((IControllerFactory)_innerFactory).GetControllerSessionBehavior(requestContext, controllerName); -// } -// -// /// -// /// Releases the specified controller. -// /// -// /// The controller. -// public virtual void ReleaseController(IController controller) -// { -// _innerFactory.ReleaseController(controller); -// } -// -// /// -// /// By default, only exposes which throws an exception -// /// if the controller is not found. Since we want to try creating a controller, and then fall back to if one isn't found, -// /// this nested class changes the visibility of 's internal methods in order to not have to rely on a try-catch. -// /// -// /// -// internal class OverridenDefaultControllerFactory : ContainerControllerFactory -// { -// public OverridenDefaultControllerFactory() -// : base(new Lazy(() => Current.Factory)) -// { } -// -// public new IController GetControllerInstance(RequestContext requestContext, Type controllerType) -// { -// return base.GetControllerInstance(requestContext, controllerType); -// } -// -// public new Type GetControllerType(RequestContext requestContext, string controllerName) -// { -// return controllerName.IsNullOrWhiteSpace() -// ? null -// : base.GetControllerType(requestContext, controllerName); -// } -// } -// } -// } diff --git a/src/Umbraco.Web/Mvc/UmbracoMvcHandler.cs b/src/Umbraco.Web/Mvc/UmbracoMvcHandler.cs deleted file mode 100644 index 0357853c86..0000000000 --- a/src/Umbraco.Web/Mvc/UmbracoMvcHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System.Web.Mvc; -using System.Web.Routing; - -namespace Umbraco.Web.Mvc -{ - /// - /// MVC handler to facilitate the TemplateRenderer. This handler can execute an MVC request and return it as a string. - /// - /// Original: - /// - /// This handler also used to intercept creation of controllers and store it for later use. - /// This was needed for the 'return CurrentUmbracoPage()' surface controller functionality - /// because it needs to send data back to the page controller. - /// - /// The creation of this controller has been moved to the UmbracoPageResult class which will create a controller when needed. - /// - internal class UmbracoMvcHandler : MvcHandler - { - public UmbracoMvcHandler(RequestContext requestContext) - : base(requestContext) - { - } - - /// - /// This is used internally purely to render an Umbraco MVC template to string and shouldn't be used for anything else. - /// - internal void ExecuteUmbracoRequest() - { - base.ProcessRequest(RequestContext.HttpContext); - } - } -} diff --git a/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs b/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs deleted file mode 100644 index 43dc341655..0000000000 --- a/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs +++ /dev/null @@ -1,130 +0,0 @@ -using System; -using System.Text; -using System.Web; -using System.Web.Mvc; -using System.Web.WebPages; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; -using Umbraco.Core; -using Umbraco.Core.Cache; -using Umbraco.Core.Configuration; -using Umbraco.Core.Configuration.Models; -using Umbraco.Core.Configuration.UmbracoSettings; -using Umbraco.Core.Models.PublishedContent; -using Umbraco.Core.Services; -using Umbraco.Core.Strings; -using Umbraco.Web.Models; -using Umbraco.Web.Routing; -using Umbraco.Web.Security; -using Current = Umbraco.Web.Composing.Current; - -namespace Umbraco.Web.Mvc -{ - // TODO: This has been ported to netcore, just needs testing - public abstract class UmbracoViewPage : WebViewPage - { - private readonly GlobalSettings _globalSettings; - private readonly ContentSettings _contentSettings; - - private IUmbracoContext _umbracoContext; - private UmbracoHelper _helper; - - /// - /// Gets or sets the database context. - /// - public ServiceContext Services { get; set; } - - /// - /// Gets or sets the application cache. - /// - public AppCaches AppCaches { get; set; } - - // TODO: previously, Services and ApplicationCache would derive from UmbracoContext.Application, which - // was an ApplicationContext - so that everything derived from UmbracoContext. - // UmbracoContext is fetched from the data tokens, thus allowing the view to be rendered with a - // custom context and NOT the Current.UmbracoContext - eg outside the normal Umbraco routing - // process. - // leaving it as-it for the time being but really - the UmbracoContext should be injected just - // like the Services & ApplicationCache properties, and have a setter for those special weird - // cases. - - // TODO: Can be injected to the view in netcore, else injected to the base model - // public IUmbracoContext UmbracoContext => _umbracoContext - // ?? (_umbracoContext = ViewContext.GetUmbracoContext() ?? Current.UmbracoContext); - - /// - /// Gets the public content request. - /// - internal IPublishedRequest PublishedRequest - { - get - { - // TODO: we only have one data token for a route now: Constants.Web.UmbracoRouteDefinitionDataToken - - throw new NotImplementedException("Probably needs to be ported to netcore"); - - //// we should always try to return the object from the data tokens just in case its a custom object and not - //// the one from UmbracoContext. Fallback to UmbracoContext if necessary. - - //// try view context - //if (ViewContext.RouteData.DataTokens.ContainsKey(Constants.Web.UmbracoRouteDefinitionDataToken)) - //{ - // return (IPublishedRequest) ViewContext.RouteData.DataTokens.GetRequiredObject(Constants.Web.UmbracoRouteDefinitionDataToken); - //} - - //// child action, try parent view context - //if (ViewContext.IsChildAction && ViewContext.ParentActionViewContext.RouteData.DataTokens.ContainsKey(Constants.Web.UmbracoRouteDefinitionDataToken)) - //{ - // return (IPublishedRequest) ViewContext.ParentActionViewContext.RouteData.DataTokens.GetRequiredObject(Constants.Web.UmbracoRouteDefinitionDataToken); - //} - - //// fallback to UmbracoContext - //return UmbracoContext.PublishedRequest; - } - } - - /// - /// Gets the Umbraco helper. - /// - public UmbracoHelper Umbraco - { - get - { - if (_helper != null) return _helper; - - var model = ViewData.Model; - var content = model as IPublishedContent; - if (content == null && model is IContentModel) - content = ((IContentModel) model).Content; - - _helper = Current.UmbracoHelper; - - if (content != null) - _helper.AssignedContentItem = content; - - return _helper; - } - } - - protected UmbracoViewPage() - : this( - Current.Factory.GetRequiredService(), - Current.Factory.GetRequiredService(), - Current.Factory.GetRequiredService>(), - Current.Factory.GetRequiredService>() - ) - { - } - - protected UmbracoViewPage(ServiceContext services, AppCaches appCaches, IOptions globalSettings, IOptions contentSettings) - { - if (globalSettings == null) throw new ArgumentNullException(nameof(globalSettings)); - if (contentSettings == null) throw new ArgumentNullException(nameof(contentSettings)); - Services = services; - AppCaches = appCaches; - _globalSettings = globalSettings.Value; - _contentSettings = contentSettings.Value; - } - - } -} diff --git a/src/Umbraco.Web/Mvc/ValidateUmbracoFormRouteStringAttribute.cs b/src/Umbraco.Web/Mvc/ValidateUmbracoFormRouteStringAttribute.cs deleted file mode 100644 index 7ee44a6abc..0000000000 --- a/src/Umbraco.Web/Mvc/ValidateUmbracoFormRouteStringAttribute.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net; -using System.Net.Http; -using System.Web.Mvc; -using Umbraco.Core; - -namespace Umbraco.Web.Mvc -{ - /// - /// Attribute used to check that the request contains a valid Umbraco form request string. - /// - /// - /// - /// - /// Applying this attribute/filter to a or SurfaceController Action will ensure that the Action can only be executed - /// when it is routed to from within Umbraco, typically when rendering a form with BeginUmbracoForm. It will mean that the natural MVC route for this Action - /// will fail with a . - /// - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] - public sealed class ValidateUmbracoFormRouteStringAttribute : FilterAttribute, IAuthorizationFilter - { - /// - /// Called when authorization is required. - /// - /// The filter context. - /// filterContext - /// The required request field \"ufprt\" is not present. - /// or - /// The Umbraco form request route string could not be decrypted. - /// or - /// The provided Umbraco form request route string was meant for a different controller and action. - public void OnAuthorization(AuthorizationContext filterContext) - { - if (filterContext == null) - throw new ArgumentNullException(nameof(filterContext)); - - var ufprt = filterContext.HttpContext.Request["ufprt"]; - ValidateRouteString(ufprt, filterContext.ActionDescriptor?.ControllerDescriptor.ControllerName, filterContext.ActionDescriptor?.ActionName, filterContext.RouteData?.DataTokens["area"]?.ToString()); - } - public void ValidateRouteString(string ufprt, string currentController, string currentAction, string currentArea) - { - if (ufprt.IsNullOrWhiteSpace()) - { - throw new HttpUmbracoFormRouteStringException("The required request field \"ufprt\" is not present."); - } - - if (!UmbracoHelper.DecryptAndValidateEncryptedRouteString(ufprt, out var additionalDataParts)) - { - throw new HttpUmbracoFormRouteStringException("The Umbraco form request route string could not be decrypted."); - } - - if (!additionalDataParts[RenderRouteHandler.ReservedAdditionalKeys.Controller].InvariantEquals(currentController) || - !additionalDataParts[RenderRouteHandler.ReservedAdditionalKeys.Action].InvariantEquals(currentAction) || - (!additionalDataParts[RenderRouteHandler.ReservedAdditionalKeys.Area].IsNullOrWhiteSpace() && !additionalDataParts[RenderRouteHandler.ReservedAdditionalKeys.Area].InvariantEquals(currentArea))) - { - throw new HttpUmbracoFormRouteStringException("The provided Umbraco form request route string was meant for a different controller and action."); - } - - } - } -} diff --git a/src/Umbraco.Web/Mvc/ViewDataContainerExtensions.cs b/src/Umbraco.Web/Mvc/ViewDataContainerExtensions.cs deleted file mode 100644 index 2852e80619..0000000000 --- a/src/Umbraco.Web/Mvc/ViewDataContainerExtensions.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System.Web.Mvc; - -namespace Umbraco.Web.Mvc -{ - internal static class ViewDataContainerExtensions - { - private class ViewDataContainer : IViewDataContainer - { - public ViewDataContainer() - { - ViewData = new ViewDataDictionary(); - } - public ViewDataDictionary ViewData { get; set; } - } - - /// - /// Creates a new IViewDataContainer but with a filtered ModelState - /// - /// - /// - /// - public static IViewDataContainer FilterContainer(this IViewDataContainer container, string prefix) - { - var newContainer = new ViewDataContainer(); - newContainer.ViewData.ModelState.Merge(container.ViewData.ModelState, prefix); - //change the HTML field name too - newContainer.ViewData.TemplateInfo.HtmlFieldPrefix = prefix; - return newContainer; - } - - /// - /// Returns a new IViewContainer based on the current one but supplies a different model to the ViewData - /// - /// - /// - /// - public static IViewDataContainer CopyWithModel(this IViewDataContainer container, object model) - { - return new ViewDataContainer - { - ViewData = new ViewDataDictionary(container.ViewData) - { - Model = model - } - }; - } - - } -} diff --git a/src/Umbraco.Web/Runtime/WebInitialComponent.cs b/src/Umbraco.Web/Runtime/WebInitialComponent.cs deleted file mode 100644 index 5abcabfd6e..0000000000 --- a/src/Umbraco.Web/Runtime/WebInitialComponent.cs +++ /dev/null @@ -1,123 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web.Http; -using System.Web.Http.Dispatcher; -using System.Web.Mvc; -using System.Web.Routing; -using Umbraco.Core; -using Umbraco.Core.Composing; -using Umbraco.Core.Configuration; -using Umbraco.Core.Configuration.Models; -using Umbraco.Core.Hosting; -using Umbraco.Core.Strings; -using Umbraco.Web.Mvc; -using Umbraco.Web.WebApi; -using Constants = Umbraco.Core.Constants; -using Current = Umbraco.Web.Composing.Current; - -namespace Umbraco.Web.Runtime -{ - public sealed class WebInitialComponent : IComponent - { - private readonly IUmbracoContextAccessor _umbracoContextAccessor; - private readonly UmbracoApiControllerTypeCollection _apiControllerTypes; - private readonly GlobalSettings _globalSettings; - private readonly IHostingEnvironment _hostingEnvironment; - private readonly IShortStringHelper _shortStringHelper; - - public WebInitialComponent( - IUmbracoContextAccessor umbracoContextAccessor, - UmbracoApiControllerTypeCollection apiControllerTypes, - GlobalSettings globalSettings, - IHostingEnvironment hostingEnvironment, - IShortStringHelper shortStringHelper) - { - _umbracoContextAccessor = umbracoContextAccessor; - _apiControllerTypes = apiControllerTypes; - _globalSettings = globalSettings; - _hostingEnvironment = hostingEnvironment; - _shortStringHelper = shortStringHelper; - } - - public void Initialize() - { - // setup mvc and webapi services - SetupMvcAndWebApi(); - - // Disable the X-AspNetMvc-Version HTTP Header - MvcHandler.DisableMvcResponseHeader = true; - - // wrap view engines in the profiling engine - WrapViewEngines(ViewEngines.Engines); - - // add global filters - ConfigureGlobalFilters(); - - // set routes - CreateRoutes(_umbracoContextAccessor, _globalSettings, _shortStringHelper, _apiControllerTypes, _hostingEnvironment); - } - - public void Terminate() - { } - - private static void ConfigureGlobalFilters() - { - //GlobalFilters.Filters.Add(new EnsurePartialViewMacroViewContextFilterAttribute()); - } - - // internal for tests - internal static void WrapViewEngines(IList viewEngines) - { - if (viewEngines == null || viewEngines.Count == 0) return; - - var originalEngines = viewEngines.ToList(); - viewEngines.Clear(); - foreach (var engine in originalEngines) - { - var wrappedEngine = engine;// TODO introduce in NETCORE: is ProfilingViewEngine ? engine : new ProfilingViewEngine(engine); - viewEngines.Add(wrappedEngine); - } - } - - private void SetupMvcAndWebApi() - { - //don't output the MVC version header (security) - //MvcHandler.DisableMvcResponseHeader = true; - - // set master controller factory - // var controllerFactory = new MasterControllerFactory(() => Current.FilteredControllerFactories); - // ControllerBuilder.Current.SetControllerFactory(controllerFactory); - - // set the render & plugin view engines - // ViewEngines.Engines.Add(new RenderViewEngine(_hostingEnvironment)); - // ViewEngines.Engines.Add(new PluginViewEngine()); - - ////add the profiling action filter - //GlobalFilters.Filters.Add(new ProfilingActionFilter()); - - GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), - new NamespaceHttpControllerSelector(GlobalConfiguration.Configuration)); - } - - // internal for tests - internal static void CreateRoutes( - IUmbracoContextAccessor umbracoContextAccessor, - GlobalSettings globalSettings, - IShortStringHelper shortStringHelper, - UmbracoApiControllerTypeCollection apiControllerTypes, - IHostingEnvironment hostingEnvironment) - { - var umbracoPath = globalSettings.GetUmbracoMvcArea(hostingEnvironment); - - // create the front-end route - var defaultRoute = RouteTable.Routes.MapRoute( - "Umbraco_default", - umbracoPath + "/RenderMvc/{action}/{id}", - new { controller = "RenderMvc", action = "Index", id = UrlParameter.Optional } - ); - - defaultRoute.RouteHandler = new RenderRouteHandler(umbracoContextAccessor, ControllerBuilder.Current.GetControllerFactory(), shortStringHelper); - } - } -} diff --git a/src/Umbraco.Web/Runtime/WebInitialComposer.cs b/src/Umbraco.Web/Runtime/WebInitialComposer.cs deleted file mode 100644 index 3f166c5747..0000000000 --- a/src/Umbraco.Web/Runtime/WebInitialComposer.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System.Web.Security; -using Microsoft.Extensions.DependencyInjection; -using Umbraco.Core; -using Umbraco.Core.DependencyInjection; -using Umbraco.Core.Composing; -using Umbraco.Core.Dictionary; -using Umbraco.Core.Templates; -using Umbraco.Core.Security; -using Umbraco.Core.Services; -using Umbraco.Web.Macros; -using Umbraco.Web.PublishedCache; -using Umbraco.Web.Security; -using Umbraco.Web.Security.Providers; - -namespace Umbraco.Web.Runtime -{ - [ComposeBefore(typeof(ICoreComposer))] - public sealed class WebInitialComposer : ComponentComposer - { - public override void Compose(IUmbracoBuilder builder) - { - base.Compose(builder); - - // register membership stuff - builder.Services.AddTransient(factory => MembershipProviderExtensions.GetMembersMembershipProvider()); - builder.Services.AddTransient(factory => Roles.Enabled ? Roles.Provider : new MembersRoleProvider(factory.GetRequiredService())); - builder.Services.AddScoped(); - builder.Services.AddTransient(factory => factory.GetRequiredService().PublishedSnapshot.Members); - builder.Services.AddUnique(); - builder.Services.AddUnique(); - - builder.Services.AddTransient(factory => - { - var state = factory.GetRequiredService(); - - if (state.Level == RuntimeLevel.Run) - { - var umbCtx = factory.GetRequiredService(); - return new UmbracoHelper(umbCtx.IsFrontEndUmbracoRequest() ? umbCtx.PublishedRequest?.PublishedContent : null, factory.GetRequiredService(), - factory.GetRequiredService(), factory.GetRequiredService(), factory.GetRequiredService()); - } - - return new UmbracoHelper(); - }); - - // configure the container for web - //composition.ConfigureForWeb(); - - //composition - /* TODO: This will depend on if we use ServiceBasedControllerActivator - see notes in Startup.cs - * You will likely need to set DefaultRenderMvcControllerType on Umbraco.Web.Composing.Current - * which is what the extension method below did previously. - */ - //.ComposeUmbracoControllers(GetType().Assembly) - //.SetDefaultRenderMvcController(); // default controller for template views - - //we need to eagerly scan controller types since they will need to be routed - // composition.WithCollectionBuilder() - // .Add(composition.TypeLoader.GetSurfaceControllers()); - - - // auto-register views - //composition.RegisterAuto(typeof(UmbracoViewPage<>)); - } - } -} - diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 5b6aa01f6d..8b4d742aeb 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -137,16 +137,10 @@ - - - - - - @@ -162,7 +156,6 @@ - @@ -179,9 +172,6 @@ - - - @@ -194,10 +184,6 @@ - - - - @@ -206,14 +192,9 @@ - - - - - True True @@ -223,11 +204,8 @@ - - - @@ -255,8 +233,6 @@ Mvc\web.config - - - + \ No newline at end of file