using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Routing;
namespace MAX.Framework.Conventions;
///
/// 控制器内未声明 HTTP 方法的操作默认只接受 GET,供各模块复用。
/// 显式 HTTP 方法声明优先;同时补充路由约束和端点元数据,保证接口文档一致。
///
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public sealed class DefaultHttpGetAttribute : 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.Get]));
selector.EndpointMetadata.Add(new HttpMethodMetadata([HttpMethods.Get]));
}
}
}