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
{
///
/// Applies the to any action on a controller that is
///
public class VirtualPageApplicationModelProvider : IApplicationModelProvider
{
private readonly List _actionModelConventions = new List()
{
new VirtualPageConvention()
};
///
///
/// Will execute after
///
public int Order => 0;
///
public void OnProvidersExecuted(ApplicationModelProviderContext context) { }
///
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();
}
}