Files
Umbraco-CMS/src/Umbraco.Web.Common/Extensions/ControllerActionEndpointConventionBuilderExtensions.cs
Mole bf41c2eeaa Netcore: Align namespaces (#9801)
* Rename Umbraco.Core namespace to Umbraco.Cms.Core

* Move extension methods in core project to Umbraco.Extensions

* Move extension methods in core project to Umbraco.Extensions

* Rename Umbraco.Examine namespace to Umbraco.Cms.Examine

* Move examine extensions to Umbraco.Extensions namespace

* Reflect changed namespaces in Builder and fix unit tests

* Adjust namespace in Umbraco.ModelsBuilder.Embedded

* Adjust namespace in Umbraco.Persistence.SqlCe

* Adjust namespace in Umbraco.PublishedCache.NuCache

* Align namespaces in Umbraco.Web.BackOffice

* Align namespaces in Umbraco.Web.Common

* Ensure that SqlCeSupport is still enabled after changing the namespace

* Align namespaces in Umbraco.Web.Website

* Align namespaces in Umbraco.Web.UI.NetCore

* Align namespaces in Umbraco.Tests.Common

* Align namespaces in Umbraco.Tests.UnitTests

* Align namespaces in Umbraco.Tests.Integration

* Fix errors caused by changed namespaces

* Fix integration tests

* Undo the Umbraco.Examine.Lucene namespace change

This breaks integration tests on linux, since the namespace wont exists there because it's only used on windows.

* Fix merge

* Fix Merge
2021-02-18 11:06:02 +01:00

53 lines
2.8 KiB
C#

using System;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Web.Common.Filters;
using Umbraco.Cms.Web.Common.Routing;
namespace Umbraco.Extensions
{
public static class ControllerActionEndpointConventionBuilderExtensions
{
/// <summary>
/// Allows for defining a callback to set the returned <see cref="IPublishedContent"/> for the current request for this route
/// </summary>
public static void ForUmbracoPage(
this ControllerActionEndpointConventionBuilder builder,
Func<ActionExecutingContext, IPublishedContent> findContent)
=> builder.Add(convention =>
{
// filter out matched endpoints that are suppressed
if (convention.Metadata.OfType<ISuppressMatchingMetadata>().FirstOrDefault()?.SuppressMatching != true)
{
// Get the controller action descriptor
ControllerActionDescriptor actionDescriptor = convention.Metadata.OfType<ControllerActionDescriptor>().FirstOrDefault();
if (actionDescriptor != null)
{
// This is more or less like the IApplicationModelProvider, it allows us to add filters, etc... to the ControllerActionDescriptor
// dynamically. Here we will add our custom virtual page filter along with a callback in the endpoint's metadata
// to execute in order to find the IPublishedContent for the request.
var filter = new UmbracoVirtualPageFilterAttribute();
// Check if this already contains this filter since we don't want it applied twice.
// This could occur if the controller being routed is IVirtualPageController AND
// is being routed with ForUmbracoPage. In that case, ForUmbracoPage wins
// because the UmbracoVirtualPageFilterAttribute will check for the metadata first since
// that is more explicit and flexible in case the same controller is routed multiple times.
if (!actionDescriptor.FilterDescriptors.Any(x => x.Filter is UmbracoVirtualPageFilterAttribute))
{
actionDescriptor.FilterDescriptors.Add(new FilterDescriptor(filter, 0));
convention.Metadata.Add(filter);
}
convention.Metadata.Add(new CustomRouteContentFinderDelegate(findContent));
}
}
});
}
}