【问题标题】:.net core 6 api bad request.net core 6 api 错误请求
【发布时间】:2022-09-30 18:07:52
【问题描述】:

过去我曾经像这样做我的api请求

    [HttpPost]
    public IActionResult CreateLead(CreateLeadRequest request)
    {
        if (request == null)
        {
            return BadRequest();
        }

        return Ok(_handler.Value.CreateLead(request));
    }

但是现在使用 .net 6 您返回实际值而不是操作结果:

    [HttpPost(\"create\", Name = nameof(CreateLead))]
    public async Task<int> CreateLead(CreateLeadRequest request)
    {
        return await _handler.Value.CreateLead(request);
    }

那么在这种情况下,当编译器抱怨BadRequest 不是int 时,我该如何返回空请求的错误结果?

    标签: c# .net-core asp.net-core-webapi .net-6.0


    【解决方案1】:

    您可以使用async Task&lt;ActionResult&lt;int&gt;&gt;。 这允许您返回 HttpStatuscodes 以及对象本身。

    【讨论】:

    • 啊,太棒了,谢谢,当它允许我时会接受
    猜你喜欢
    • 2022-11-17
    • 2019-04-11
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多