From b86e8bcc1e51b48ed5dfc6a5dec9a4739487fba0 Mon Sep 17 00:00:00 2001 From: Damiaan Peeters Date: Fri, 19 Nov 2021 13:03:38 +0100 Subject: [PATCH] Following example for custom routes (work in progress) --- .../Custom-Routes/Product.cs | 16 +++ .../Custom-Routes/ShopController.cs | 98 +++++++++++++++++++ Umbraco.Docs.Samples.Web/Startup.cs | 5 + 3 files changed, 119 insertions(+) create mode 100644 Umbraco.Docs.Samples.Web/Custom-Routes/Product.cs create mode 100644 Umbraco.Docs.Samples.Web/Custom-Routes/ShopController.cs diff --git a/Umbraco.Docs.Samples.Web/Custom-Routes/Product.cs b/Umbraco.Docs.Samples.Web/Custom-Routes/Product.cs new file mode 100644 index 0000000..30c2a27 --- /dev/null +++ b/Umbraco.Docs.Samples.Web/Custom-Routes/Product.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using Umbraco.Cms.Core.Models; +using Umbraco.Cms.Core.Models.PublishedContent; + +namespace Umbraco.Docs.Samples.Web.Custom_Routes +{ + public class Product : ContentModel + { + public Product(IPublishedContent content) : base(content) + { + } + + public string Sku { get; set; } + public IEnumerable AvailableStores { get; set; } + } +} \ No newline at end of file diff --git a/Umbraco.Docs.Samples.Web/Custom-Routes/ShopController.cs b/Umbraco.Docs.Samples.Web/Custom-Routes/ShopController.cs new file mode 100644 index 0000000..a7d6d6c --- /dev/null +++ b/Umbraco.Docs.Samples.Web/Custom-Routes/ShopController.cs @@ -0,0 +1,98 @@ +using System.Linq; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.AspNetCore.Mvc.Filters; +using Microsoft.AspNetCore.Mvc.ViewEngines; +using Microsoft.Extensions.Logging; +using Umbraco.Cms.Core.Models.PublishedContent; +using Umbraco.Cms.Core.Web; +using Umbraco.Cms.Web.Common.Controllers; +using Umbraco.Extensions; + +namespace Umbraco.Docs.Samples.Web.Custom_Routes +{ + public class ShopController : UmbracoPageController, IVirtualPageController + { + private readonly IUmbracoContextAccessor _umbracoContextAccessor; + private readonly IPublishedValueFallback _publishedValueFallback; + + public ShopController( + ILogger logger, + ICompositeViewEngine compositeViewEngine, + IUmbracoContextAccessor umbracoContextAccessor, + IPublishedValueFallback publishedValueFallback) + : base(logger, compositeViewEngine) + { + _umbracoContextAccessor = umbracoContextAccessor; + _publishedValueFallback = publishedValueFallback; + } + + public IPublishedContent FindContent(ActionExecutingContext actionExecutingContext) + { + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); + + if (umbracoContext == null) + { + return null; + } + + var productRoot = umbracoContext.Content.GetById(2074); + if (actionExecutingContext.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) + { + return productRoot; + } + + // Check which action is executing + switch (controllerActionDescriptor.ActionName) + { + case nameof(Index): + return productRoot; + + case nameof(Product): + // Get the SKU/Id from the route values + if (actionExecutingContext.ActionArguments.TryGetValue("id", out var sku)) + { + return productRoot + .Children + .FirstOrDefault(c => c.Value(_publishedValueFallback, "sku") == sku.ToString()); + } + else + { + return productRoot; + } + } + + return productRoot; + } + + [HttpGet] + public IActionResult Index() + { + // CurrentPage (IPublishedContent) will be the content returned + // from the FindContent method. + + // return the view with the IPublishedContent + return View(CurrentPage); + } + + [HttpGet] + public IActionResult Product(string id) + { + // CurrentPage (IPublishedContent) will be the content returned + // from the FindContent method. + + // One example of using a custom route would be to include additional + // model information based on external services. For example, if + // we wanted to return the stores the product is available in from + // a custom data store. + var dbProduct = DbContext.Products.GetBySku(id); + var shopModel = new Product(CurrentPage) + { + Sku = id, + AvailableStores = dbProduct.AvailableStores + }; + + return View(shopModel); + } + } +} \ No newline at end of file diff --git a/Umbraco.Docs.Samples.Web/Startup.cs b/Umbraco.Docs.Samples.Web/Startup.cs index 9973342..bb20af4 100644 --- a/Umbraco.Docs.Samples.Web/Startup.cs +++ b/Umbraco.Docs.Samples.Web/Startup.cs @@ -79,6 +79,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) }) .WithEndpoints(u => { + u.EndpointRouteBuilder.MapControllerRoute( + "Shop Controller", + "/shop/{action}/{id?}", + new { Controller = "Shop", Action = "Index" }); + u.UseInstallerEndpoints(); u.UseBackOfficeEndpoints(); u.UseWebsiteEndpoints();