Move all routing tokens (incl. API versioning) to Web.Common (#14231)

* Move all routing tokens (incl. API versioning) to Cms.Web.Common, so the site can start without adding the delivery API in Startup

* Fixed merge

* Fix backwards compat
This commit is contained in:
Kenn Jacobsen
2023-05-11 08:18:43 +02:00
committed by GitHub
parent f804c8c209
commit f4ee0d027a
14 changed files with 43 additions and 49 deletions

View File

@@ -1,28 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Api.Common.Routing;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
namespace Umbraco.Cms.Api.Common.Configuration;
public class ConfigureMvcOptions : IConfigureOptions<MvcOptions>
{
private readonly IOptions<GlobalSettings> _globalSettings;
public ConfigureMvcOptions(IOptions<GlobalSettings> globalSettings) => _globalSettings = globalSettings;
public void Configure(MvcOptions options)
{
// these MVC options may be applied more than once; let's make sure we only execute once.
if (options.Conventions.Any(convention => convention is UmbracoBackofficeToken))
{
return;
}
// Replace the BackOfficeToken in routes.
var backofficePath = _globalSettings.Value.UmbracoPath.TrimStart(Constants.CharArrays.TildeForwardSlash);
options.Conventions.Add(new UmbracoBackofficeToken(Constants.Web.AttributeRouting.BackOfficeToken, backofficePath));
}
}

View File

@@ -22,10 +22,6 @@ public static class UmbracoBuilderApiExtensions
{
public static IUmbracoBuilder AddUmbracoApiOpenApiUI(this IUmbracoBuilder builder)
{
builder.Services.ConfigureOptions<ConfigureApiVersioningOptions>();
builder.Services.ConfigureOptions<ConfigureApiExplorerOptions>();
builder.Services.AddApiVersioning().AddApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.ConfigureOptions<ConfigureUmbracoSwaggerGenOptions>();
builder.Services.AddSingleton<IUmbracoJsonTypeInfoResolver, UmbracoJsonTypeInfoResolver>();

View File

@@ -8,9 +8,7 @@
<RootNamespace>Umbraco.Cms.Api.Common</RootNamespace>
</PropertyGroup>
<ItemGroup>
<!-- <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.8" />-->
<PackageReference Include="Asp.Versioning.Mvc" Version="7.0.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -34,7 +34,6 @@ public static class UmbracoBuilderExtensions
builder
.Services
.ConfigureOptions<ConfigureMvcOptions>()
.AddControllers()
.AddJsonOptions(Constants.JsonOptionsNames.DeliveryApi, options =>
{

View File

@@ -14,19 +14,20 @@ internal sealed class ValidateStartItemAttribute : TypeFilterAttribute
private class ValidateStartItemFilter : IActionFilter
{
private readonly IRequestStartItemProvider _requestStartItemProvider;
private readonly IRequestStartItemProviderAccessor _requestStartItemProviderAccessor;
public ValidateStartItemFilter(IRequestStartItemProvider requestStartItemProvider)
=> _requestStartItemProvider = requestStartItemProvider;
public ValidateStartItemFilter(IRequestStartItemProviderAccessor requestStartItemProviderAccessor)
=> _requestStartItemProviderAccessor = requestStartItemProviderAccessor;
public void OnActionExecuting(ActionExecutingContext context)
{
if (_requestStartItemProvider.RequestedStartItem() is null)
if (_requestStartItemProviderAccessor.TryGetValue(out IRequestStartItemProvider? requestStartItemProvider) is false
|| requestStartItemProvider.RequestedStartItem() is null)
{
return;
}
IPublishedContent? startItem = _requestStartItemProvider.GetStartItem();
IPublishedContent? startItem = requestStartItemProvider.GetStartItem();
if (startItem is null)
{

View File

@@ -1,4 +1,4 @@
using Umbraco.Cms.Api.Common.Routing;
using Umbraco.Cms.Web.Common.Routing;
namespace Umbraco.Cms.Api.Delivery.Routing;

View File

@@ -13,6 +13,7 @@
<ItemGroup>
<ProjectReference Include="..\Umbraco.Cms.Api.Common\Umbraco.Cms.Api.Common.csproj" />
<ProjectReference Include="..\Umbraco.Infrastructure\Umbraco.Infrastructure.csproj" />
<ProjectReference Include="..\Umbraco.Web.Common\Umbraco.Web.Common.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -2,7 +2,7 @@ using Asp.Versioning;
using Asp.Versioning.ApiExplorer;
using Microsoft.Extensions.Options;
namespace Umbraco.Cms.Api.Common.Configuration;
namespace Umbraco.Cms.Web.Common.Configuration;
public sealed class ConfigureApiExplorerOptions : IConfigureOptions<ApiExplorerOptions>
{

View File

@@ -1,8 +1,7 @@
using Asp.Versioning;
using Microsoft.Extensions.Options;
namespace Umbraco.Cms.Api.Common.Configuration;
namespace Umbraco.Cms.Web.Common.Configuration;
public sealed class ConfigureApiVersioningOptions : IConfigureOptions<ApiVersioningOptions>
{

View File

@@ -46,6 +46,7 @@ using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
using Umbraco.Cms.Web.Common;
using Umbraco.Cms.Web.Common.ApplicationModels;
using Umbraco.Cms.Web.Common.AspNetCore;
using Umbraco.Cms.Web.Common.Configuration;
using Umbraco.Cms.Web.Common.Controllers;
using Umbraco.Cms.Web.Common.DependencyInjection;
using Umbraco.Cms.Web.Common.FileProviders;
@@ -291,6 +292,9 @@ public static partial class UmbracoBuilderExtensions
options.Cookie.HttpOnly = true;
});
builder.Services.ConfigureOptions<ConfigureApiVersioningOptions>();
builder.Services.ConfigureOptions<ConfigureApiExplorerOptions>();
builder.Services.AddApiVersioning().AddApiExplorer();
builder.Services.ConfigureOptions<UmbracoMvcConfigureOptions>();
builder.Services.ConfigureOptions<UmbracoRequestLocalizationOptions>();
builder.Services.TryAddEnumerable(ServiceDescriptor

View File

@@ -1,7 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Web.Common.Filters;
using Umbraco.Cms.Web.Common.ModelBinders;
using Umbraco.Cms.Web.Common.Routing;
using Umbraco.Cms.Web.Common.Validators;
namespace Umbraco.Cms.Web.Common.Mvc;
@@ -15,6 +19,17 @@ namespace Umbraco.Cms.Web.Common.Mvc;
/// </remarks>
public class UmbracoMvcConfigureOptions : IConfigureOptions<MvcOptions>
{
private readonly GlobalSettings _globalSettings;
[Obsolete("Use the constructor that accepts GlobalSettings options. Will be removed in V14.")]
public UmbracoMvcConfigureOptions()
: this(StaticServiceProvider.Instance.GetRequiredService<IOptions<GlobalSettings>>())
{
}
public UmbracoMvcConfigureOptions(IOptions<GlobalSettings> globalSettings)
=> _globalSettings = globalSettings.Value;
/// <inheritdoc />
public void Configure(MvcOptions options)
{
@@ -22,5 +37,13 @@ public class UmbracoMvcConfigureOptions : IConfigureOptions<MvcOptions>
options.ModelValidatorProviders.Insert(0, new BypassRenderingModelValidatorProvider());
options.ModelMetadataDetailsProviders.Add(new BypassRenderingModelValidationMetadataProvider());
options.Filters.Insert(0, new EnsurePartialViewMacroViewContextFilterAttribute());
// these MVC options may be applied more than once; let's make sure we only add these conventions once.
if (options.Conventions.Any(convention => convention is UmbracoBackofficeToken) is false)
{
// Replace the BackOfficeToken in routes.
var backofficePath = _globalSettings.UmbracoPath.TrimStart(Core.Constants.CharArrays.TildeForwardSlash);
options.Conventions.Add(new UmbracoBackofficeToken(Core.Constants.Web.AttributeRouting.BackOfficeToken, backofficePath));
}
}
}

View File

@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core;
namespace Umbraco.Cms.Api.Common.Routing;
namespace Umbraco.Cms.Web.Common.Routing;
/// <summary>
/// Routes a controller within the backoffice area, I.E /umbraco
@@ -11,7 +10,7 @@ public class BackOfficeRouteAttribute : RouteAttribute
// All this does is append [umbracoBackoffice]/ to the route,
// this is then replaced with whatever is configures as UmbracoPath by the UmbracoBackofficeToken convention
public BackOfficeRouteAttribute(string template)
: base($"[{Constants.Web.AttributeRouting.BackOfficeToken}]/" + template.TrimStart('/'))
: base($"[{Core.Constants.Web.AttributeRouting.BackOfficeToken}]/" + template.TrimStart('/'))
{
}
}

View File

@@ -1,7 +1,7 @@
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
namespace Umbraco.Cms.Api.Common.Routing;
namespace Umbraco.Cms.Web.Common.Routing;
/// <summary>
/// Adds a custom template token for specifying backoffice route with attribute routing

View File

@@ -11,6 +11,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Asp.Versioning.Mvc" Version="7.0.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="7.0.0" />
<PackageReference Include="Dazinator.Extensions.FileProviders" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.2" />