You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FastEndpoints无请求体带路由参数与Header的PATCH接口问题

FastEndpoints PATCH接口无请求体+路由/Header参数解决方案

问题根源

使用Endpoint<TestRequest>时,框架默认判定接口需要请求体,curl调用未传body就会返回415错误;换成EndpointWithoutRequest又没法自动绑定Header参数,导致Swagger也无法正确展示必填Header。

方案一:给请求类标记无请求体(推荐)

给请求类添加[NoRequestBody]特性,让框架明确该接口不需要请求体,同时保留自动绑定路由和Header参数的能力,Swagger也能正常渲染参数。

完整代码:

using FastEndpoints;
using System.ComponentModel.DataAnnotations;

public class TestRequest
{
    [FromHeader("x-correlation-id")]
    [Required] // 标记为必填Header
    public Guid CorrelationId { get; set; }

    [BindFrom("EntityId")] // 绑定路由中的EntityId参数
    [Required]
    public Guid EntityId { get; set; }
}

public sealed class TestEndpoint : Endpoint<TestRequest>
{
    public override void Configure()
    {
        Patch("{EntityId}/fulfill");
        // 按需设置权限,比如AllowAnonymous或自定义授权策略
        AllowAnonymous();
        // 可选:给Swagger接口分组
        Options(x => x.WithTags("测试接口"));
        // 确保Swagger不显示请求体区域
        Description(x =>
        {
            x.ClearRequestBody();
            x.Produces(200);
            x.ProducesProblem(400); // 展示可能的参数验证错误
        });
    }

    public override async Task HandleAsync(TestRequest req, CancellationToken cancellationToken)
    {
        // 直接使用req中的CorrelationId和EntityId
        await SendOkAsync(cancellationToken);
    }
}

方案二:用EndpointWithoutRequest手动绑定参数

如果不想定义请求类,可以手动在端点内获取路由和Header参数,同时配置Swagger的参数展示。

完整代码:

using FastEndpoints;
using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations;

public sealed class TestEndpoint : EndpointWithoutRequest
{
    public override void Configure()
    {
        Patch("{EntityId}/fulfill");
        AllowAnonymous();
        Options(x => x.WithTags("测试接口"));
        // 手动配置Swagger展示的参数
        Description(x =>
        {
            x.AddHeaderParameter<Guid>("x-correlation-id", "关联ID", isRequired: true);
            x.AddRouteParameter<Guid>("EntityId", "实体ID", isRequired: true);
            x.Produces(200);
            x.ProducesProblem(400);
        });
    }

    public override async Task HandleAsync(CancellationToken cancellationToken)
    {
        // 获取路由参数
        var entityId = Route<Guid>("EntityId");
        
        // 获取并验证Header参数
        if (!HttpContext.Request.Headers.TryGetValue("x-correlation-id", out var correlationIdStr) ||
            !Guid.TryParse(correlationIdStr, out var correlationId))
        {
            // 返回参数验证失败响应
            await SendValidationFailureAsync(new[]
            {
                new ValidationFailure("x-correlation-id", "必填的关联ID格式不正确")
            }, cancellationToken);
            return;
        }

        // 执行业务逻辑
        await SendOkAsync(cancellationToken);
    }
}

验证调用

两种方案都支持你提供的curl命令直接调用,不会返回415错误:

curl -X 'PATCH' \
  '.../72423509-c29b-4735-862a-9ebdd0489c05/fulfill' \
  -H 'x-correlation-id: 72423509-c29b-4735-862a-9ebdd0489c05'

内容的提问来源于stack exchange,提问作者Pt4r

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 09:36:13