Dipper/Dipper.Alioth/Web/StarletConvention.cs

141 lines
4.3 KiB
C#
Raw Permalink Normal View History

2022-11-06 23:03:52 +08:00
using System.Text;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
2022-11-06 22:42:32 +08:00
using Microsoft.AspNetCore.Mvc.ApplicationModels;
namespace Dipper.Alioth.Web;
2022-11-27 17:36:19 +08:00
public class StarletConvention : IApplicationModelConvention
2022-11-06 22:42:32 +08:00
{
public void Apply(ApplicationModel application)
{
2022-11-06 23:03:52 +08:00
foreach (var controller in application.Controllers)
{
var type = controller.ControllerType;
2022-11-27 17:23:54 +08:00
if (typeof(IStarlet).IsAssignableFrom(type))
2022-11-06 23:03:52 +08:00
{
ConfigureServiceExplorer(controller);
ConfigureSelector(controller);
}
}
2022-11-06 22:42:32 +08:00
}
2022-11-06 23:03:52 +08:00
private static void ConfigureServiceExplorer(ControllerModel controller)
{
controller.ApiExplorer.IsVisible ??= true;
foreach (var action in controller.Actions)
{
action.ApiExplorer.IsVisible ??= true;
}
}
private void ConfigureSelector(ControllerModel controller)
{
RemoveEmptySelectors(controller.Selectors);
if (controller.Selectors.Any(selector => selector.AttributeRouteModel != null))
return;
foreach (var action in controller.Actions)
{
ConfigureSelector(action);
}
}
private static void RemoveEmptySelectors(IList<SelectorModel> selectors)
{
for (var i = selectors.Count - 1; i >= 0; i--)
{
var selector = selectors[i];
if (selector.AttributeRouteModel == null &&
selector.ActionConstraints.Count <= 0 &&
selector.EndpointMetadata.Count <= 0)
{
selectors.Remove(selector);
}
}
}
private void ConfigureSelector(ActionModel action)
{
RemoveEmptySelectors(action.Selectors);
if (action.Selectors.Count <= 0)
AddServiceSelector(action);
else
NormalizeSelectorRoutes(action);
}
private void AddServiceSelector(ActionModel action)
{
var template = new Microsoft.AspNetCore.Mvc.RouteAttribute(GetRouteTemplate(action));
var selector = new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel(template)
};
selector.ActionConstraints.Add(new HttpMethodActionConstraint(new[] { GetHttpMethod(action) }));
action.Selectors.Add(selector);
}
private void NormalizeSelectorRoutes(ActionModel action)
{
foreach (var selector in action.Selectors)
{
if (selector.AttributeRouteModel == null)
{
var template = new Microsoft.AspNetCore.Mvc.RouteAttribute(GetRouteTemplate(action));
selector.AttributeRouteModel = new AttributeRouteModel(template);
}
if (selector.ActionConstraints.OfType<HttpMethodActionConstraint>().FirstOrDefault()?.HttpMethods?.FirstOrDefault() == null)
selector.ActionConstraints.Add(new HttpMethodActionConstraint(new[] { GetHttpMethod(action) }));
}
}
private static string GetRouteTemplate(ActionModel action)
{
if (action.Attributes is { Count: > 0})
{
foreach (var item in action.Attributes)
{
2022-11-06 23:05:39 +08:00
if (item is Microsoft.AspNetCore.Mvc.RouteAttribute attribute)
2022-11-06 23:03:52 +08:00
{
// attribute.Path
2022-11-06 23:05:39 +08:00
return attribute.Template;
2022-11-06 23:03:52 +08:00
}
}
}
var routeTemplate = new StringBuilder();
//routeTemplate.Append("api");
var names = action.Controller.ControllerType.Namespace.Split('.');
if (names.Length > 2)
{
routeTemplate.Append(names[^2]);
}
// Controller
var controllerName = action.Controller.ControllerName;
if (controllerName.EndsWith("Service"))
controllerName = controllerName[0..^7];
routeTemplate.Append($"/{controllerName}");
// Action
var actionName = action.ActionName;
if (actionName.EndsWith("Async"))
actionName = actionName[..^"Async".Length];
if (!string.IsNullOrEmpty(actionName))
routeTemplate.Append($"/{actionName}");
return routeTemplate.ToString();
}
private static string GetHttpMethod(ActionModel action)
{
var actionName = action.ActionName;
return actionName.StartsWith("Get") ? "GET" : "POST";
}
2022-11-06 22:42:32 +08:00
}