using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Routing;
namespace MAX.Framework.Conventions;
///
/// 控制器内未声明 HTTP 方法的操作默认只接受 POST。
/// 同时补充动作约束和端点元数据,使路由匹配与 OpenAPI 文档保持一致;显式方法声明优先。
///
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public sealed class DefaultHttpPostAttribute : Attribute, IControllerModelConvention
{
public void Apply(ControllerModel controller)
{
foreach (var action in controller.Actions)
foreach (var selector in action.Selectors)
{
if (selector.ActionConstraints.OfType().Any()
|| selector.EndpointMetadata.OfType().Any())
continue;
selector.ActionConstraints.Add(new HttpMethodActionConstraint([HttpMethods.Post]));
selector.EndpointMetadata.Add(new HttpMethodMetadata([HttpMethods.Post]));
}
}
}