2021-11-30 15:35:50 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
2021-12-01 13:03:06 +01:00
|
|
|
using System.Reflection;
|
2021-11-30 15:35:50 +01:00
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace zero.Api.Controllers;
|
|
|
|
|
|
|
|
|
|
public class ZeroApiControllerModelConvention : IControllerModelConvention
|
|
|
|
|
{
|
2021-12-13 12:21:15 +01:00
|
|
|
protected Type BaseClassType { get; set; } = typeof(ZeroApiController);
|
|
|
|
|
|
2021-12-01 13:03:06 +01:00
|
|
|
readonly AttributeRouteModel AppAwareRouteModel;
|
|
|
|
|
|
|
|
|
|
readonly AttributeRouteModel AppUnawareRouteModel;
|
2021-11-30 15:35:50 +01:00
|
|
|
|
2021-12-13 12:21:15 +01:00
|
|
|
readonly Type SystemApiType = typeof(ZeroSystemApiAttribute);
|
|
|
|
|
|
|
|
|
|
readonly ApiParameterTransformer Transformer = new();
|
2021-11-30 15:35:50 +01:00
|
|
|
|
2021-12-01 13:03:06 +01:00
|
|
|
readonly bool RuntimeIsAppAware = false;
|
|
|
|
|
|
2021-11-30 15:35:50 +01:00
|
|
|
|
2021-12-13 12:21:15 +01:00
|
|
|
public ZeroApiControllerModelConvention(string path, bool isAppAware = false)
|
2021-11-30 15:35:50 +01:00
|
|
|
{
|
2021-12-01 13:03:06 +01:00
|
|
|
RuntimeIsAppAware = isAppAware;
|
2021-12-13 12:21:15 +01:00
|
|
|
AppAwareRouteModel = BuildRouteModel(path, true);
|
|
|
|
|
AppUnawareRouteModel = BuildRouteModel(path, false);
|
2021-11-30 15:35:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Configure routing model for all backoffice controllers
|
|
|
|
|
/// </summary>
|
2021-12-03 14:45:49 +01:00
|
|
|
public virtual void Apply(ControllerModel controller)
|
2021-11-30 15:35:50 +01:00
|
|
|
{
|
|
|
|
|
bool hasAttributeRouteModels = controller.Selectors.Any(selector => selector.AttributeRouteModel != null);
|
|
|
|
|
|
2021-12-13 12:21:15 +01:00
|
|
|
if (controller.ControllerType.IsSubclassOf(BaseClassType))
|
2021-11-30 15:35:50 +01:00
|
|
|
{
|
2021-12-13 12:21:15 +01:00
|
|
|
bool isAppAware = RuntimeIsAppAware && controller.ControllerType.GetCustomAttribute(SystemApiType) == null;
|
2021-12-01 13:03:06 +01:00
|
|
|
|
|
|
|
|
controller.Selectors[0].AttributeRouteModel = isAppAware ? AppAwareRouteModel : AppUnawareRouteModel;
|
2021-12-13 12:21:15 +01:00
|
|
|
controller.Filters.Add(new DisableBrowserCacheFilterAttribute());
|
|
|
|
|
|
|
|
|
|
foreach (var action in controller.Actions)
|
|
|
|
|
{
|
|
|
|
|
action.RouteParameterTransformer = Transformer;
|
|
|
|
|
}
|
2021-11-30 15:35:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-01 13:03:06 +01:00
|
|
|
|
|
|
|
|
|
2021-12-13 14:28:25 +01:00
|
|
|
protected virtual AttributeRouteModel BuildRouteModel(string pathSegment, bool appAware = false)
|
2021-12-01 13:03:06 +01:00
|
|
|
{
|
|
|
|
|
StringBuilder path = new();
|
2021-12-13 12:21:15 +01:00
|
|
|
path.Append(pathSegment.EnsureSurroundedWith('/'));
|
2021-12-01 13:03:06 +01:00
|
|
|
|
2021-12-13 14:28:25 +01:00
|
|
|
path.Append("{zero_app_key}/");
|
|
|
|
|
// TODO add route constraint which only allows registered app-ids
|
|
|
|
|
// see https://nemi-chand.github.io/creating-custom-routing-constraint-in-aspnet-core-mvc/
|
2021-12-01 13:03:06 +01:00
|
|
|
|
|
|
|
|
path.Append("[controller]");
|
|
|
|
|
|
|
|
|
|
return new AttributeRouteModel(new RouteAttribute(path.ToString()));
|
|
|
|
|
}
|
2021-11-30 15:35:50 +01:00
|
|
|
}
|