Merge remote-tracking branch 'origin/netcore/dev' into netcore/feature/align-namespaces

# Conflicts:
#	src/Umbraco.Web/Mvc/SurfaceController.cs
This commit is contained in:
Mole
2021-02-12 08:40:36 +01:00
7 changed files with 0 additions and 202 deletions

View File

@@ -198,7 +198,6 @@
<Compile Include="Routing\RoutesCacheTests.cs" />
<Compile Include="Routing\UrlRoutingTestBase.cs" />
<Compile Include="Web\PublishedContentQueryTests.cs" />
<Compile Include="Web\Mvc\MergeParentContextViewDataAttributeTests.cs" />
<Compile Include="Web\Mvc\ViewDataDictionaryExtensionTests.cs" />
<Compile Include="Persistence\Querying\ContentTypeSqlMappingTests.cs" />
<Compile Include="PublishedContent\PublishedContentExtensionTests.cs" />

View File

@@ -1,86 +0,0 @@
using System.Web.Mvc;
using System.Web.Routing;
using NUnit.Framework;
using Umbraco.Tests.TestHelpers;
using Umbraco.Web.Mvc;
namespace Umbraco.Tests.Web.Mvc
{
[TestFixture]
public class MergeParentContextViewDataAttributeTests
{
[Test]
public void Ensure_All_Ancestor_ViewData_Is_Merged()
{
var http = new FakeHttpContextFactory("http://localhost");
//setup an heirarchy
var rootViewCtx = new ViewContext {Controller = new MyController(), RequestContext = http.RequestContext, ViewData = new ViewDataDictionary()};
var parentViewCtx = new ViewContext { Controller = new MyController(), RequestContext = http.RequestContext, RouteData = new RouteData(), ViewData = new ViewDataDictionary() };
parentViewCtx.RouteData.DataTokens.Add("ParentActionViewContext", rootViewCtx);
var controllerCtx = new ControllerContext(http.RequestContext, new MyController()) {RouteData = new RouteData()};
controllerCtx.RouteData.DataTokens.Add("ParentActionViewContext", parentViewCtx);
//set up the view data
controllerCtx.Controller.ViewData["Test1"] = "Test1";
controllerCtx.Controller.ViewData["Test2"] = "Test2";
controllerCtx.Controller.ViewData["Test3"] = "Test3";
parentViewCtx.ViewData["Test4"] = "Test4";
parentViewCtx.ViewData["Test5"] = "Test5";
parentViewCtx.ViewData["Test6"] = "Test6";
rootViewCtx.ViewData["Test7"] = "Test7";
rootViewCtx.ViewData["Test8"] = "Test8";
rootViewCtx.ViewData["Test9"] = "Test9";
var filter = new ResultExecutingContext(controllerCtx, new ContentResult()) {RouteData = controllerCtx.RouteData};
var att = new MergeParentContextViewDataAttribute();
Assert.IsTrue(filter.IsChildAction);
att.OnResultExecuting(filter);
Assert.AreEqual(9, controllerCtx.Controller.ViewData.Count);
}
[Test]
public void Ensure_All_Ancestor_ViewData_Is_Merged_Without_Data_Loss()
{
var http = new FakeHttpContextFactory("http://localhost");
//setup an heirarchy
var rootViewCtx = new ViewContext { Controller = new MyController(), RequestContext = http.RequestContext, ViewData = new ViewDataDictionary() };
var parentViewCtx = new ViewContext { Controller = new MyController(), RequestContext = http.RequestContext, RouteData = new RouteData(), ViewData = new ViewDataDictionary() };
parentViewCtx.RouteData.DataTokens.Add("ParentActionViewContext", rootViewCtx);
var controllerCtx = new ControllerContext(http.RequestContext, new MyController()) { RouteData = new RouteData() };
controllerCtx.RouteData.DataTokens.Add("ParentActionViewContext", parentViewCtx);
//set up the view data with overlapping keys
controllerCtx.Controller.ViewData["Test1"] = "Test1";
controllerCtx.Controller.ViewData["Test2"] = "Test2";
controllerCtx.Controller.ViewData["Test3"] = "Test3";
parentViewCtx.ViewData["Test2"] = "Test4";
parentViewCtx.ViewData["Test3"] = "Test5";
parentViewCtx.ViewData["Test4"] = "Test6";
rootViewCtx.ViewData["Test3"] = "Test7";
rootViewCtx.ViewData["Test4"] = "Test8";
rootViewCtx.ViewData["Test5"] = "Test9";
var filter = new ResultExecutingContext(controllerCtx, new ContentResult()) { RouteData = controllerCtx.RouteData };
var att = new MergeParentContextViewDataAttribute();
Assert.IsTrue(filter.IsChildAction);
att.OnResultExecuting(filter);
Assert.AreEqual(5, controllerCtx.Controller.ViewData.Count);
Assert.AreEqual("Test1", controllerCtx.Controller.ViewData["Test1"]);
Assert.AreEqual("Test2", controllerCtx.Controller.ViewData["Test2"]);
Assert.AreEqual("Test3", controllerCtx.Controller.ViewData["Test3"]);
Assert.AreEqual("Test6", controllerCtx.Controller.ViewData["Test4"]);
Assert.AreEqual("Test9", controllerCtx.Controller.ViewData["Test5"]);
}
internal class MyController : Controller
{
}
}
}

View File

@@ -17,9 +17,6 @@ namespace Umbraco.Cms.Web.Website.Controllers
/// <summary>
/// Provides a base class for front-end add-in controllers.
/// </summary>
// TODO: Migrate MergeModelStateToChildAction and MergeParentContextViewData action filters
// [MergeModelStateToChildAction]
// [MergeParentContextViewData]
[AutoValidateAntiforgeryToken]
public abstract class SurfaceController : PluginController
{

View File

@@ -1,42 +0,0 @@
using System.Linq;
using System.Web.Mvc;
namespace Umbraco.Web.Mvc
{
/// <summary>
/// When a ChildAction is executing and we want the ModelState from the Parent context to be merged in
/// to help with validation, this filter can be used.
/// </summary>
/// <remarks>
/// By default, this filter will only merge when an Http POST is detected but this can be modified in the ctor
/// </remarks>
public class MergeModelStateToChildActionAttribute : ActionFilterAttribute
{
private readonly string[] _verb;
public MergeModelStateToChildActionAttribute()
: this(HttpVerbs.Post)
{
}
public MergeModelStateToChildActionAttribute(params HttpVerbs[] verb)
{
_verb = verb.Select(x => x.ToString().ToUpper()).ToArray();
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//check if the verb matches, if so merge the ModelState before the action is executed.
if (_verb.Contains(filterContext.HttpContext.Request.HttpMethod))
{
if (filterContext.Controller.ControllerContext.IsChildAction)
{
filterContext.Controller.ViewData.ModelState.Merge(
filterContext.Controller.ControllerContext.ParentActionViewContext.ViewData.ModelState);
}
}
base.OnActionExecuting(filterContext);
}
}
}

View File

@@ -1,56 +0,0 @@
using System.Linq;
using System.Web.Mvc;
namespace Umbraco.Web.Mvc
{
/// <summary>
/// This attribute can be used for when child actions execute and will automatically merge in the viewdata from the parent context to the
/// child action result.
/// </summary>
/// <remarks>
/// This will retain any custom viewdata put into the child viewdata if the same key persists in the parent context's view data. You can always still
/// access the parent's view data normally.
/// This just simplifies working with ChildActions and view data.
///
/// NOTE: This does not mean that the parent context's view data will be merged before the action executes, if you need access to the parent context's view
/// data during controller execution you can access it normally.
///
/// NOTE: This recursively merges in all ParentActionViewContext ancestry in case there's child actions inside of child actions.
/// </remarks>
public class MergeParentContextViewDataAttribute : ActionFilterAttribute
{
/// <summary>
/// Merge in the parent context's view data if this is a child action when the result is being executed
/// </summary>
/// <param name="filterContext"></param>
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
if (filterContext.IsChildAction)
{
MergeCurrentParent(filterContext.Controller, filterContext.ParentActionViewContext);
}
base.OnResultExecuting(filterContext);
}
/// <summary>
/// Recursively merges in each parent view context into the target
/// </summary>
/// <param name="target"></param>
/// <param name="currentParent"></param>
private static void MergeCurrentParent(ControllerBase target, ViewContext currentParent)
{
if (currentParent != null && currentParent.ViewData != null && currentParent.ViewData.Any())
{
target.ViewData.MergeViewDataFrom(currentParent.ViewData);
//Recurse!
if (currentParent.IsChildAction)
{
MergeCurrentParent(target, currentParent.ParentActionViewContext);
}
}
}
}
}

View File

@@ -1,22 +1,10 @@
using System;
using Umbraco.Core;
using System.Collections.Specialized;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Core.Cache;
using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Services;
using Umbraco.Web.Composing;
namespace Umbraco.Web.Mvc
{
/// Migrated already to .Net Core without MergeModelStateToChildAction and MergeParentContextViewData action filters
/// TODO: Migrate MergeModelStateToChildAction and MergeParentContextViewData action filters
[MergeModelStateToChildAction]
[MergeParentContextViewData]
public abstract class SurfaceController : PluginController
{
protected SurfaceController()

View File

@@ -176,7 +176,6 @@
<Compile Include="Mvc\EnsurePublishedContentRequestAttribute.cs" />
<Compile Include="Mvc\UmbracoVirtualNodeRouteHandler.cs" />
<Compile Include="Security\AuthenticationOptionsExtensions.cs" />
<Compile Include="Mvc\MergeParentContextViewDataAttribute.cs" />
<Compile Include="Mvc\ViewDataDictionaryExtensions.cs" />
<Compile Include="Security\MembershipHelper.cs" />
<Compile Include="HttpCookieExtensions.cs" />
@@ -193,7 +192,6 @@
<Compile Include="WebApi\UmbracoApiController.cs" />
<Compile Include="WebApi\UmbracoApiControllerBase.cs" />
<Compile Include="ModelStateExtensions.cs" />
<Compile Include="Mvc\MergeModelStateToChildActionAttribute.cs" />
<Compile Include="Mvc\PluginController.cs" />
<Compile Include="Mvc\Strings.Designer.cs">
<AutoGen>True</AutoGen>