【发布时间】:2012-08-31 14:38:26
【问题描述】:
起初它有效,但今天它失败了!
这就是我定义日期属性的方式:
[Display(Name = "Date")]
[Required(ErrorMessage = "Date of Submission is required.")]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
[DataType(DataType.Date)]
public DateTime TripDate { get; set; }
过去一直有效。但是今天,当我调用相同的 ApiController 操作时:
[HttpPost]
public HttpResponseMessage SaveNewReport(TripLeaderReportInputModel model)
Firebug 报告:
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"
发生了什么?那些[DataContract] 不是WCF 吗?我在MVC4 中使用REST WebAPI!
有人可以帮忙吗?请问?
---更新---
我找到了一些类似的链接。
MvC 4.0 RTM broke us and we don't know how to fix it RSS
---再次更新---
这是 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
请求标头:
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
---更新---
我找到了一个临时解决方案。请参阅下面的答案。如果有人理解它为什么起作用或有更好的解决方案,请发布您的答案。谢谢。
【问题讨论】:
-
你改变你的称呼了吗?
-
我不这么认为。什么变化会出现这样的异常?重新申请
[DataContract]标签? -
在以前不需要序列化内容的情况下需要序列化内容的任何更改。
-
我发现
Json.NET已安装在 NuGet 中。这是我应该删除的那个吗? -
不,我无法删除
Json.NET。无法卸载“Newtonsoft.Json 4.5.8”,因为“Jang 1.0.1, Microsoft.AspNet.WebApi.Client 4.0.20710.0”依赖于它。
标签: asp.net-mvc-4 asp.net-web-api data-annotations