【问题标题】:Add a message to your HTTP 400 Bad Request in ASP.NET Core 3.1在 ASP.NET Core 3.1 中向 HTTP 400 错误请求添加消息
【发布时间】:2020-07-29 13:21:11
【问题描述】:

有没有一种方法可以将消息添加到 BadRequest 操作结果,并使该消息对外部客户端(例如 Postman)可见?我正在使用 ASP.NET Core 3.1。

我的部分代码包含在下面。我想说问题是什么——例如,在正文中发送的id 与从 URL 中获取的不一样。目前,我正在使用我制作的 Error 对象,其中包含错误代码和消息。但是当我发送请求时,这些在 Postman 中不可见。

public ActionResult PutColour(int id, Colour colour)
{
    if (id != colour.Id)
    {
        return BadRequest(new Error("IDNotTheSame","ID from URL is not the same as in the body."));
    }
}

【问题讨论】:

    标签: asp.net-core httprequest asp.net-core-webapi asp.net-core-3.1 bad-request


    【解决方案1】:

    您传递给BadRequest 的内容被序列化并作为响应正文返回。如果 什么都没有 通过,唯一的解释是您在 Error 上没有任何可以序列化的公共属性。例如,如果你有类似的东西:

    public class Error
    {
        public Error(string type, string description)
        {
            Type = type;
            Description = description;
        }
    
        public string Type { get; private set }
        public string Description { get; private set; }
    }
    

    然后,您会收到如下响应:

    {
        "type": "IDNotTheSame",
        "description": "ID from URL is not the same as in the body."
    }
    

    不确定您的 Error 课程目前在做什么。但是,无论如何这可能是不必要的,因为您可以使用ModelState

    ModelState.AddModelError("Id", "ID from URL is not the same as in the body.");
    return BadRequest(ModelState);
    

    最后,应该说这首先是一个毫无意义的验证。您根本不应该发送带有模型的 id(始终使用视图模型,而不是实体类),即使您发送了它,您也可以简单地用 URL 中的值覆盖它:

    model.Id = id;
    

    完成。没有问题,您无需担心发回错误。

    【讨论】:

    • 非常感谢!是的,问题是在我的错误类中我有属性,而不是属性。另外,感谢您提供的额外信息,非常有帮助。我会上网查看更多关于视图模型的信息,但我理解你写给我的想法
    • 我还要感谢 Jeremy Caney,他让问题更正确 :) 不知道这是否是正确的地方,但是嘿,我试过了 :)
    【解决方案2】:

    请按照以下步骤使用您的自定义类显示错误请求。 在此处创建该类的构造函数。并添加手动描述等等

    第 1 步

    添加一个自定义类并用ValidationProblemDetails 继承这个类。之后通过传递context.ModelState 在基类构造函数下面初始化。

    public ValidationProblemDetails(ModelStateDictionary modelState)

    这里是实现-

    public class CustomBadRequest : ValidationProblemDetails
        {
            public CustomBadRequest(ActionContext context) : base(context.ModelState)
            {
                Detail = "add details here";
                Instance = "add extension here";
                Status = 400;
                Title = "add title here";
                Type = "add type here";
            }
        }
    
    

    第 2 步

    Startup.cs 中的ConfigureServices 方法中添加此自定义错误请求类。

    public void ConfigureServices(IServiceCollection services)
            {
    services.AddMvc().ConfigureApiBehaviorOptions(options =>
                {
                    options.InvalidModelStateResponseFactory = context =>
                    {
                        var problems = new CustomBadRequest(context);
    
                        return new BadRequestObjectResult(problems);
                    };
                });
    }
    

    第 3 步

    构建并运行。

    如果出现任何错误请求,您将看到如下输出-

    【讨论】:

      猜你喜欢
      • 2013-07-21
      • 2021-06-02
      • 2017-07-21
      • 2016-11-18
      • 1970-01-01
      • 2018-10-08
      • 2020-10-12
      • 2020-11-15
      • 1970-01-01
      相关资源
      最近更新 更多