Bla*_*ise 34 data-annotations asp.net-mvc-4 asp.net-web-api
首先它有效,但今天它失败了!
这是我定义date属性的方法:
[Display(Name = "Date")]
[Required(ErrorMessage = "Date of Submission is required.")]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime TripDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
它一直在工作.但今天,当我调用相同的ApiController动作时:
[HttpPost]
public HttpResponseMessage SaveNewReport(TripLeaderReportInputModel model)
Run Code Online (Sandbox Code Playgroud)
萤火虫报道:
ExceptionMessage:
"Property 'TripDate' on type 'Whitewater.ViewModels.Report.TripLeaderReportInputModel'
is invalid. Value-typed properties marked as [Required] must also be marked with
[DataMember(IsRequired=true)] to be recognized as required. Consider attributing the
declaring type with [DataContract] and the property with [DataMember(IsRequired=true)]."
ExceptionType
"System.InvalidOperationException"
Run Code Online (Sandbox Code Playgroud)
发生了什么?是不是那些[DataContract]对WCF?我正在使用REST WebAPI中MVC4!
有人可以帮忙吗?请?
---更新---
我发现了一些类似的链接.
---再次更新---
这是HTTP响应标头:
Cache-Control no-cache
Connection Close
Content-Length 1846
Content-Type application/json; charset=utf-8
Date Thu, 06 Sep 2012 17:48:15 GMT
Expires -1
Pragma no-cache
Server ASP.NET Development Server/10.0.0.0
X-AspNet-Version 4.0.30319
Run Code Online (Sandbox Code Playgroud)
请求标题:
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Cache-Control no-cache
Connection keep-alive
Content-Length 380
Content-Type application/x-www-form-urlencoded; charset=UTF-8
Cookie .ASPXAUTH=1FF35BD017B199BE629A2408B2A3DFCD4625F9E75D0C58BBD0D128D18FFDB8DA3CDCB484C80176A74C79BB001A20201C6FB9B566FEE09B1CF1D8EA128A67FCA6ABCE53BB7D80B634A407F9CE2BE436BDE3DCDC2C3E33AAA2B4670A0F04DAD13A57A7ABF600FA80C417B67C53BE3F4D0EACE5EB125BD832037E392D4ED4242CF6
DNT 1
Host localhost:39019
Pragma no-cache
Referer http://localhost:39019/Report/TripLeader
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0
X-Requested-With XMLHttpRequest
Run Code Online (Sandbox Code Playgroud)
---更新---
我找到了一个临时解决方案.见下面的答案.如果有人理解为什么它有效或有更好的解决方案,请发表您的答案.谢谢.
Bla*_*ise 34
好的.虽然我还没有完全理解这件事.找到了解决方法.
在Global.asax:
GlobalConfiguration.Configuration.Services.RemoveAll(
typeof(System.Web.Http.Validation.ModelValidatorProvider),
v => v is InvalidModelValidatorProvider);
Run Code Online (Sandbox Code Playgroud)
我在aspnetwebstack的问题跟踪器中找到了它.这是该页面的链接:
将[DataMember(IsRequired = true)]应用于具有值类型的必需属性的过度激进的验证
如果有人能告诉我们为什么会这样,请将您的见解发布为答案.谢谢.
Bla*_*ise 13
我添加了一个ModelValidationFilterAttribute并使其工作:
public class ModelValidationFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (!actionContext.ModelState.IsValid)
{
// Return the validation errors in the response body.
var errors = new Dictionary<string, IEnumerable<string>>();
//string key;
foreach (KeyValuePair<string, ModelState> keyValue in actionContext.ModelState)
{
//key = keyValue.Key.Substring(keyValue.Key.IndexOf('.') + 1);
errors[keyValue.Key] = keyValue.Value.Errors.Select(e => e.ErrorMessage);
}
//var errors = actionContext.ModelState
// .Where(e => e.Value.Errors.Count > 0)
// .Select(e => new Error
// {
// Name = e.Key,
// Message = e.Value.Errors.First().ErrorMessage
// }).ToArray();
actionContext.Response =
actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, errors);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以[ModelValidation]在操作上添加过滤器.或者将其添加到Global.asax.cs:
GlobalConfiguration.Configuration.Services.RemoveAll(
typeof(System.Web.Http.Validation.ModelValidatorProvider),
v => v is InvalidModelValidatorProvider);
Run Code Online (Sandbox Code Playgroud)
这样,我继续使用原始数据注释.
Mar*_*ers 10
更新24-5-2013:已从 ASP.NET技术堆栈中删除InvalidModelValidatorProvider了对此错误消息的责任.该验证器证明引起的混淆比它要解决的更多.有关更多信息,请参阅以下链接:http://aspnetwebstack.codeplex.com/workitem/270
使用[DataContract]属性装饰类时,需要显式装饰要使用[DataMember]属性序列化的成员.
问题是DataContractSerializer不支持该[Required]属性.对于引用类型,我们可以在反序列化后检查该值是否为null.但是对于值类型,也没有办法为我们执行[Required]语义DataContractSerializer没有[DataMember(IsRequired=true)].
因此,如果未发送,则最终可能会标记为DateTimeas [Required]并期望模型验证错误DateTime,但您只需获取DateTime.MinValue值并且不会出现验证错误.
| 归档时间: |
|
| 查看次数: |
15483 次 |
| 最近记录: |