Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
Add AdminShared view fallback branch to ViewLocationExpander (ARCH-001 Phase 2)
KrzysztofPajakd9ba03806d4b
diff --git a/src/Tests/Grand.Web.Common.Tests/View/AdminSharedControllersNamespaceFakes.cs b/src/Tests/Grand.Web.Common.Tests/View/AdminSharedControllersNamespaceFakes.csnew file mode 100644index 000000000..89748bb9a--- /dev/null+++ b/src/Tests/Grand.Web.Common.Tests/View/AdminSharedControllersNamespaceFakes.cs@@ -0,0 +1,14 @@+// Stands in for a real Base*Controller living in Grand.Web.AdminShared.Controllers+// (this test project has no reference to Grand.Web.AdminShared) — only the+// namespace string matters to ViewLocationExpander.IsAdminSharedController.+namespace Grand.Web.AdminShared.Controllers+{+ public abstract class FakeBaseController { }+}++namespace Grand.Web.Common.Tests.View+{+ public class FakeAdminSharedSubclass : Grand.Web.AdminShared.Controllers.FakeBaseController { }++ public class FakeUnrelatedController { }+}diff --git a/src/Tests/Grand.Web.Common.Tests/View/ViewLocationExpanderTests.cs b/src/Tests/Grand.Web.Common.Tests/View/ViewLocationExpanderTests.csnew file mode 100644index 000000000..d6aad6812--- /dev/null+++ b/src/Tests/Grand.Web.Common.Tests/View/ViewLocationExpanderTests.cs@@ -0,0 +1,35 @@+using System.Reflection;+using Grand.Web.Common.View;+using Microsoft.AspNetCore.Mvc.Abstractions;+using Microsoft.AspNetCore.Mvc.Controllers;+using Microsoft.VisualStudio.TestTools.UnitTesting;++namespace Grand.Web.Common.Tests.View;++[TestClass]+public class ViewLocationExpanderTests+{+ private static ControllerActionDescriptor DescriptorFor(Type controllerType) =>+ new() { ControllerTypeInfo = controllerType.GetTypeInfo() };++ [TestMethod]+ public void IsAdminSharedController_TypeDerivesFromAdminSharedControllersNamespace_ReturnsTrue()+ {+ var descriptor = DescriptorFor(typeof(FakeAdminSharedSubclass));+ Assert.IsTrue(ViewLocationExpander.IsAdminSharedController(descriptor));+ }++ [TestMethod]+ public void IsAdminSharedController_UnrelatedType_ReturnsFalse()+ {+ var descriptor = DescriptorFor(typeof(FakeUnrelatedController));+ Assert.IsFalse(ViewLocationExpander.IsAdminSharedController(descriptor));+ }++ [TestMethod]+ public void IsAdminSharedController_NonControllerActionDescriptor_ReturnsFalse()+ {+ var descriptor = new ActionDescriptor();+ Assert.IsFalse(ViewLocationExpander.IsAdminSharedController(descriptor));+ }+}diff --git a/src/Web/Grand.Web.Common/Grand.Web.Common.csproj b/src/Web/Grand.Web.Common/Grand.Web.Common.csprojindex 607719f89..53d8c644a 100644--- a/src/Web/Grand.Web.Common/Grand.Web.Common.csproj+++ b/src/Web/Grand.Web.Common/Grand.Web.Common.csproj@@ -1,5 +1,8 @@<Project Sdk="Microsoft.NET.Sdk"><Import Project="..\..\Build\Grand.Common.props" />+ <ItemGroup>+ <InternalsVisibleTo Include="Grand.Web.Common.Tests" />+ </ItemGroup><ItemGroup><PackageReference Include="Azure.Extensions.AspNetCore.DataProtection.Blobs" /><PackageReference Include="Azure.Extensions.AspNetCore.DataProtection.Keys" />diff --git a/src/Web/Grand.Web.Common/View/ViewLocationExpander.cs b/src/Web/Grand.Web.Common/View/ViewLocationExpander.csindex 3fcada461..ff3b04b27 100644--- a/src/Web/Grand.Web.Common/View/ViewLocationExpander.cs+++ b/src/Web/Grand.Web.Common/View/ViewLocationExpander.cs@@ -1,4 +1,6 @@-using Grand.Web.Common.Themes;+using Grand.Web.Common.Themes;+using Microsoft.AspNetCore.Mvc.Abstractions;+using Microsoft.AspNetCore.Mvc.Controllers;using Microsoft.AspNetCore.Mvc.Razor;using Microsoft.Extensions.DependencyInjection;@@ -7,6 +9,8 @@ namespace Grand.Web.Common.View;public class ViewLocationExpander : IViewLocationExpander{private const string ThemeKey = "Theme";+ private const string AdminSharedFallbackLocation = "/Views/{1}/{0}.cshtml";+ private const string AdminSharedControllersNamespace = "Grand.Web.AdminShared.Controllers";public void PopulateValues(ViewLocationExpanderContext context){@@ -21,11 +25,28 @@ public class ViewLocationExpander : IViewLocationExpanderpublic IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context,IEnumerable<string> viewLocations){- if (!context.Values.TryGetValue(ThemeKey, out var _)) return viewLocations;+ if (context.Values.TryGetValue(ThemeKey, out _))+ {+ var viewFactory = context.ActionContext.HttpContext.RequestServices.GetRequiredService<IViewFactory>();+ viewFactory.GetViewPath(context.AreaName ?? "", ref viewLocations);+ }- var viewFactory = context.ActionContext.HttpContext.RequestServices.GetRequiredService<IViewFactory>();- viewFactory.GetViewPath(context.AreaName ?? "", ref viewLocations);+ if (IsAdminSharedController(context.ActionContext.ActionDescriptor))+ viewLocations = viewLocations.Append(AdminSharedFallbackLocation);return viewLocations;}-}\ No newline at end of file++ /// <summary>Whether the executing action's controller type (or any base type) lives in+ /// Grand.Web.AdminShared.Controllers. Generic by design — no per-entity base-controller+ /// list to maintain: the moment a future Base*Controller (Order, Category, ...) lands in+ /// that namespace, its host subclasses get the AdminShared view fallback automatically.</summary>+ internal static bool IsAdminSharedController(ActionDescriptor descriptor)+ {+ if (descriptor is not ControllerActionDescriptor cad) return false;+ for (var t = cad.ControllerTypeInfo.AsType(); t is not null; t = t.BaseType)+ if (t.Namespace == AdminSharedControllersNamespace)+ return true;+ return false;+ }+}