自定义API响应数据注释

发布于 2025-01-16 15:57:43 字数 869 浏览 2 评论 0原文

我目前正在构建一个 Web API,当 DataAnnotation Validation 出现错误时,我在显示响应时遇到问题

这是代码:

public class NewPatient
{
  [StringLength(13)]
  [Required(ErrorMessage = "Format Nomor Kartu Tidak Sesuai")]
  public string nomorkartu { get; set; }
  
  [StringLength(16)]
  [Required(ErrorMessage = "Format NIK Tidak Sesuai")]
  public string nik { get; set; }
}

当验证运行时,上面的验证返回如下响应:

{
  "errors": {
    "nomorkartu": [
      "Format Nomor Kartu Tidak Sesuai."
    ]
  },
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "00-5dc8a15c7bf9dfe1e99d3312fbfe16f7-368da3671328539c-00"
}

如何显示格式就像下面的回复:

{
  "metadata":{
    "code":"400",
    "message": "Format Nomor Kartu Tidak Sesuai."
  }
}

谢谢

I'm currently building a Web API, I'm having problems displaying a response when there is an error in DataAnnotation Validation

Here is the code:

public class NewPatient
{
  [StringLength(13)]
  [Required(ErrorMessage = "Format Nomor Kartu Tidak Sesuai")]
  public string nomorkartu { get; set; }
  
  [StringLength(16)]
  [Required(ErrorMessage = "Format NIK Tidak Sesuai")]
  public string nik { get; set; }
}

When the validation runs, the Validation above returns a response like this:

{
  "errors": {
    "nomorkartu": [
      "Format Nomor Kartu Tidak Sesuai."
    ]
  },
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "00-5dc8a15c7bf9dfe1e99d3312fbfe16f7-368da3671328539c-00"
}

How can I display a format like the following response:

{
  "metadata":{
    "code":"400",
    "message": "Format Nomor Kartu Tidak Sesuai."
  }
}

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温暖的光 2025-01-23 15:57:43

您可以通过在启动中添加以下代码来做到这一点。

public class NewPatient
{
  [StringLength(13)]
  [Required(ErrorMessage = "Format Nomor Kartu Tidak Sesuai")]
  public string nomorkartu { get; set; }
  
  [StringLength(16)]
  [Required(ErrorMessage = "Format NIK Tidak Sesuai")]
  public string nik { get; set; }
}

在启动中添加代码

// 添加模型验证错误的自定义响应

services.AddMvcCore().ConfigureApiBehaviorOptions(options => {
            options.InvalidModelStateResponseFactory = (errorContext) =>
            {
                var errors = errorContext.ModelState.Values.SelectMany(e => e.Errors.Select(m => new
                {
                    ErrorMessage = m.ErrorMessage
                })).ToList();
                var result = new
                {
                    Metadata = new
                    {
                        Code = (int)HttpStatusCode.BadRequest,
                        Message = errors.Select(e => e.ErrorMessage).ToList()
                    }
                };
                return new BadRequestObjectResult(result);
            };
        });

You can do that by adding the following code in the startup.

public class NewPatient
{
  [StringLength(13)]
  [Required(ErrorMessage = "Format Nomor Kartu Tidak Sesuai")]
  public string nomorkartu { get; set; }
  
  [StringLength(16)]
  [Required(ErrorMessage = "Format NIK Tidak Sesuai")]
  public string nik { get; set; }
}

Add the code in the startup

// Added custom response for modle validation error

services.AddMvcCore().ConfigureApiBehaviorOptions(options => {
            options.InvalidModelStateResponseFactory = (errorContext) =>
            {
                var errors = errorContext.ModelState.Values.SelectMany(e => e.Errors.Select(m => new
                {
                    ErrorMessage = m.ErrorMessage
                })).ToList();
                var result = new
                {
                    Metadata = new
                    {
                        Code = (int)HttpStatusCode.BadRequest,
                        Message = errors.Select(e => e.ErrorMessage).ToList()
                    }
                };
                return new BadRequestObjectResult(result);
            };
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文