自定义API响应数据注释
我目前正在构建一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过在启动中添加以下代码来做到这一点。
在启动中添加代码
// 添加模型验证错误的自定义响应
You can do that by adding the following code in the startup.
Add the code in the startup
// Added custom response for modle validation error