Jed*_*Jed 5 c# asp.net-mvc asp.net-web-api2
所以我有一个具有这样的控制器post动作的API :
public HttpResponseMessage Post(Model m){
if(!ModelState.IsValid){
return ApiRequest.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模型:
[MetadataType(typeof(Metadata))]
public partial class MyModel {
private sealed class Metadata {
[Required(ErrorMessage="Sample error msg.")]
public string Field1 {get; set;}
// ...other fields and foreign key fields here
}
}
Run Code Online (Sandbox Code Playgroud)
正如我在标题上所述,我首先使用数据库,这就是为什么我创建了一个partial类,因为我的模型是自动生成的.
问题是当发生验证错误时,错误消息的结果JSON如下所示:
{
e.Field1: "Error msg here...",
e.Field2: "Error msg here...",
e.DateField: "An error occurred" //This happens for any date type field even though i've specified an error message
e: ["An error occurred", "An error occurred"] //This happens for the foreign key fields i.e. AnotherModelID, I got 2 which is why I think it has two error messages also
}
Run Code Online (Sandbox Code Playgroud)
首先我想要的是删除JSON结果的e前缀keys.那么为什么An error occurred即使我明确地输出错误消息也会返回错误?还有为什么外键字段不显示前缀e?
谢谢
UPDATE
经过更多的研究,我认为在错误key只是字母e而不是e.FieldName相关字段的数据类型的情况下发生的情况下int.我已经测试了一个非外键int字段,它还添加了"e only"键下的错误.
| 归档时间: |
|
| 查看次数: |
267 次 |
| 最近记录: |