29 lines
1.1 KiB
C#
29 lines
1.1 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc.ActionConstraints;
|
|
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
namespace MAX.Framework.Conventions;
|
|
|
|
/// <summary>
|
|
/// 控制器内未声明 HTTP 方法的操作默认只接受 POST。
|
|
/// 同时补充动作约束和端点元数据,使路由匹配与 OpenAPI 文档保持一致;显式方法声明优先。
|
|
/// </summary>
|
|
[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<HttpMethodActionConstraint>().Any()
|
|
|| selector.EndpointMetadata.OfType<IHttpMethodMetadata>().Any())
|
|
continue;
|
|
|
|
selector.ActionConstraints.Add(new HttpMethodActionConstraint([HttpMethods.Post]));
|
|
selector.EndpointMetadata.Add(new HttpMethodMetadata([HttpMethods.Post]));
|
|
}
|
|
}
|
|
}
|