Gets virtual page routing working, now just need to document.
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Umbraco.Web.Common.Attributes;
|
||||
|
||||
namespace Umbraco.Web.Common.ApplicationModels
|
||||
{
|
||||
|
||||
// TODO: This should just exist in the back office project
|
||||
|
||||
/// <summary>
|
||||
@@ -13,45 +14,43 @@ namespace Umbraco.Web.Common.ApplicationModels
|
||||
/// </summary>
|
||||
public class BackOfficeApplicationModelProvider : IApplicationModelProvider
|
||||
{
|
||||
public BackOfficeApplicationModelProvider(IModelMetadataProvider modelMetadataProvider)
|
||||
private readonly List<IActionModelConvention> _actionModelConventions = new List<IActionModelConvention>()
|
||||
{
|
||||
ActionModelConventions = new List<IActionModelConvention>()
|
||||
{
|
||||
new BackOfficeIdentityCultureConvention()
|
||||
};
|
||||
}
|
||||
new BackOfficeIdentityCultureConvention()
|
||||
};
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Will execute after <see cref="DefaultApplicationModelProvider"/>
|
||||
/// </summary>
|
||||
public int Order => 0;
|
||||
|
||||
public List<IActionModelConvention> ActionModelConventions { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void OnProvidersExecuted(ApplicationModelProviderContext context)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void OnProvidersExecuting(ApplicationModelProviderContext context)
|
||||
{
|
||||
foreach (var controller in context.Result.Controllers)
|
||||
foreach (ControllerModel controller in context.Result.Controllers)
|
||||
{
|
||||
if (!IsBackOfficeController(controller))
|
||||
continue;
|
||||
|
||||
foreach (var action in controller.Actions)
|
||||
{
|
||||
foreach (var convention in ActionModelConventions)
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (ActionModel action in controller.Actions)
|
||||
{
|
||||
foreach (IActionModelConvention convention in _actionModelConventions)
|
||||
{
|
||||
convention.Apply(action);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsBackOfficeController(ControllerModel controller)
|
||||
=> controller.Attributes.OfType<IsBackOfficeAttribute>().Any();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Umbraco.Web.Common.Filters;
|
||||
|
||||
namespace Umbraco.Web.Common.ApplicationModels
|
||||
@@ -8,9 +8,7 @@ namespace Umbraco.Web.Common.ApplicationModels
|
||||
|
||||
public class BackOfficeIdentityCultureConvention : IActionModelConvention
|
||||
{
|
||||
public void Apply(ActionModel action)
|
||||
{
|
||||
action.Filters.Add(new BackOfficeCultureFilter());
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public void Apply(ActionModel action) => action.Filters.Add(new BackOfficeCultureFilter());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Web.Common.Attributes;
|
||||
|
||||
@@ -27,13 +27,18 @@ namespace Umbraco.Web.Common.ApplicationModels
|
||||
/// </remarks>
|
||||
public class UmbracoApiBehaviorApplicationModelProvider : IApplicationModelProvider
|
||||
{
|
||||
private readonly List<IActionModelConvention> _actionModelConventions;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UmbracoApiBehaviorApplicationModelProvider"/> class.
|
||||
/// </summary>
|
||||
public UmbracoApiBehaviorApplicationModelProvider(IModelMetadataProvider modelMetadataProvider)
|
||||
{
|
||||
// see see https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1#apicontroller-attribute
|
||||
// for what these things actually do
|
||||
// NOTE: we don't have attribute routing requirements and we cannot use ApiVisibilityConvention without attribute routing
|
||||
|
||||
ActionModelConventions = new List<IActionModelConvention>()
|
||||
_actionModelConventions = new List<IActionModelConvention>()
|
||||
{
|
||||
new ClientErrorResultFilterConvention(), // Ensures the responses without any body is converted into a simple json object with info instead of a string like "Status Code: 404; Not Found"
|
||||
new ConsumesConstraintForFormFileParameterConvention(), // If an controller accepts files, it must accept multipart/form-data.
|
||||
@@ -47,32 +52,33 @@ namespace Umbraco.Web.Common.ApplicationModels
|
||||
// TODO: Need to determine exactly how this affects errors
|
||||
var defaultErrorType = typeof(ProblemDetails);
|
||||
var defaultErrorTypeAttribute = new ProducesErrorResponseTypeAttribute(defaultErrorType);
|
||||
ActionModelConventions.Add(new ApiConventionApplicationModelConvention(defaultErrorTypeAttribute));
|
||||
_actionModelConventions.Add(new ApiConventionApplicationModelConvention(defaultErrorTypeAttribute));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
/// <summary>
|
||||
/// Will execute after <see cref="DefaultApplicationModelProvider"/>
|
||||
/// </summary>
|
||||
public int Order => 0;
|
||||
|
||||
public List<IActionModelConvention> ActionModelConventions { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void OnProvidersExecuted(ApplicationModelProviderContext context)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void OnProvidersExecuting(ApplicationModelProviderContext context)
|
||||
{
|
||||
foreach (var controller in context.Result.Controllers)
|
||||
{
|
||||
if (!IsUmbracoApiController(controller))
|
||||
{
|
||||
continue;
|
||||
|
||||
|
||||
}
|
||||
|
||||
foreach (var action in controller.Actions)
|
||||
{
|
||||
foreach (var convention in ActionModelConventions)
|
||||
foreach (var convention in _actionModelConventions)
|
||||
{
|
||||
convention.Apply(action);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using System.Linq;
|
||||
using Umbraco.Web.Common.Attributes;
|
||||
using Umbraco.Web.Actions;
|
||||
using Umbraco.Web.Common.Filters;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Umbraco.Web.Common.ModelBinders;
|
||||
|
||||
namespace Umbraco.Web.Common.ApplicationModels
|
||||
@@ -16,14 +13,13 @@ namespace Umbraco.Web.Common.ApplicationModels
|
||||
/// </remarks>
|
||||
public class UmbracoJsonModelBinderConvention : IActionModelConvention
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void Apply(ActionModel action)
|
||||
{
|
||||
foreach (var p in action.Parameters.Where(p => p.BindingInfo?.BindingSource == BindingSource.Body))
|
||||
foreach (ParameterModel p in action.Parameters.Where(p => p.BindingInfo?.BindingSource == BindingSource.Body))
|
||||
{
|
||||
p.BindingInfo.BinderType = typeof(UmbracoJsonModelBinder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Models.PublishedContent;
|
||||
using Umbraco.Web.Common.Controllers;
|
||||
|
||||
namespace Umbraco.Web.Common.ApplicationModels
|
||||
{
|
||||
/// <summary>
|
||||
/// Applies the <see cref="VirtualPageConvention"/> to any action on a controller that is <see cref="IVirtualPageController"/>
|
||||
/// </summary>
|
||||
public class VirtualPageApplicationModelProvider : IApplicationModelProvider
|
||||
{
|
||||
private readonly List<IActionModelConvention> _actionModelConventions = new List<IActionModelConvention>()
|
||||
{
|
||||
new VirtualPageConvention()
|
||||
};
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Will execute after <see cref="DefaultApplicationModelProvider"/>
|
||||
/// </summary>
|
||||
public int Order => 0;
|
||||
|
||||
/// <inheritdoc />
|
||||
public void OnProvidersExecuted(ApplicationModelProviderContext context) { }
|
||||
|
||||
/// <inheritdoc />
|
||||
public void OnProvidersExecuting(ApplicationModelProviderContext context)
|
||||
{
|
||||
foreach (ControllerModel controller in context.Result.Controllers)
|
||||
{
|
||||
if (!IsVirtualPageController(controller))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (ActionModel action in controller.Actions.ToList())
|
||||
{
|
||||
if (action.ActionName == nameof(IVirtualPageController.FindContent)
|
||||
&& action.ActionMethod.ReturnType == typeof(IPublishedContent))
|
||||
{
|
||||
// this is not an action, it's just the implementation of IVirtualPageController
|
||||
controller.Actions.Remove(action);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (IActionModelConvention convention in _actionModelConventions)
|
||||
{
|
||||
convention.Apply(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsVirtualPageController(ControllerModel controller)
|
||||
=> controller.ControllerType.Implements<IVirtualPageController>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Umbraco.Web.Common.Controllers;
|
||||
using Umbraco.Web.Common.Filters;
|
||||
|
||||
namespace Umbraco.Web.Common.ApplicationModels
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds the <see cref="UmbracoVirtualPageFilterAttribute"/> as a convention
|
||||
/// </summary>
|
||||
public class VirtualPageConvention : IActionModelConvention
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public void Apply(ActionModel action) => action.Filters.Add(new UmbracoVirtualPageFilterAttribute());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user