使用模型绑定和验证问题asp.net的Web API属性 [英] Problems with Model binding and validation attributes with asp.net Web API
问题描述
我写与的ASP.NET Web API上的Web API,并利用下面的视图模型。
我似乎有与数据绑定的问题时,有一个特定的属性两个验证属性(即[要求]和[StringLength(10)])。
在发布来自客户端的JSON值到窗体的控制器动作:
// POST API /列表
公共无效后([FromBody] TaskViewModel taskVM)
我注意以下事项:
- 如果我删除了多重属性之一,一切束缚OK;
- 如果我在多个属性离开时,客户端临危500内部服务器错误,永远达不到邮政法的主体。
任何想法,为什么出现这种情况?
干杯
公共类TaskViewModel
{ //默认的构造函数
公共TaskViewModel(){}
公共静态TaskViewModel MakeTaskViewModel(任务任务)
{
返回新TaskViewModel(任务);
} //构造
私人TaskViewModel(任务任务)
{
this.TaskId = task.TaskID;
this.Description = task.Description;
this.StartDate = task.StartDate;
this.Status = task.Status;
this.ListID = task.ListID;
} 公众的Guid TASKID {搞定;组; } [需要]
[StringLength(10)]
公共字符串描述{搞定;组; } [需要]
[数据类型(DataType.DateTime)
公共System.DateTime的起始日期{搞定;组; } [需要]
公共字符串状态{搞定;组; } 公众的System.Guid ListID {搞定;组; }
}
您需要检查的是在500内部服务器内部
- 请确保您打开 customerror 关闭在你的web.config
- 如果您selfhost web.API你需要设置
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
- 使用浏览器的开发控制台的网络选项卡(IE,Chrome浏览器就可以得到与F12控制台),或者如果您使用的是Firefox,然后使用萤火或THRID第三方工具一样的提琴手。
这时你可以看到什么地方出了错在服务器上,并进一步去解决你的问题。
在你的情况,这是在响应:
消息:发生错误,ExceptionMessage:物业
上键入'起始日期'MvcApplication3.Controllers.TaskViewModel'是
无效。 值类型属性标记为[必填]也必须是
标有[数据成员(IsRequired = TRUE)被确认为
要求。考虑归因声明类型[DataContract]
而财产
[数据成员(IsRequired = TRUE)],ExceptionType:System.InvalidOperationException
块引用>所以你的问题不在于你有两个属性,但已与
标记你的属性[必需]
来解决这个异常会告诉你该怎么做。您需要添加
[数据成员(IsRequired = TRUE)]
您需要的属性所在的属性类型是值类型(例如int,DATATIME等):因此,改变你的
TaskViewModel
来:[DataContract]
公共类TaskViewModel
{ //默认的构造函数
公共TaskViewModel(){} [数据成员]
公众的Guid TASKID {搞定;组; } [需要]
[数据成员]
[StringLength(10)]
公共字符串描述{搞定;组; } [需要]
[数据成员(IsRequired =真)]
[数据类型(DataType.DateTime)
公共System.DateTime的起始日期{搞定;组; } [需要]
[数据成员]
公共字符串状态{搞定;组; } [数据成员]
公众的System.Guid ListID {搞定;组; }
}一些旁注:
- 您需要引用System.Runtime.Serialization DLL以使用
DataMemberAttribute
- 您需要标记与
类[DataContract]
键,您需要使用[数据成员] $ C $,以纪念其所有属性C>不只是所需的项目。
I am writing a Web API with ASP.NET Web API, and make use of the following View Model. I seem to be having a problem with the data binding when there are two validation attributes on a particular property (i.e. [Required] and [StringLength(10)]).
When posting a JSON value from a client to a controller action of the form:
// POST api/list
public void Post([FromBody] TaskViewModel taskVM)
I observe the following:
- If I remove one of the multiple attributes everything is bound OK;
- If I leave in the multiple attributes, the client recieves a 500 internal server error and the body of the Post method is never reached.
Any ideas why this happens? Cheers
public class TaskViewModel { //Default Constructor public TaskViewModel() { } public static TaskViewModel MakeTaskViewModel(Task task) { return new TaskViewModel(task); } //Constructor private TaskViewModel(Task task) { this.TaskId = task.TaskID; this.Description = task.Description; this.StartDate = task.StartDate; this.Status = task.Status; this.ListID = task.ListID; } public Guid TaskId { get; set; } [Required] [StringLength(10)] public string Description { get; set; } [Required] [DataType(DataType.DateTime)] public System.DateTime StartDate { get; set; } [Required] public string Status { get; set; } public System.Guid ListID { get; set; } }
解决方案You need to inspect what is inside in the 500 internal server
- make sure that you turn customerror off in your web.config
- If you selfhost web.API you need to set
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
- use your browser development console's network tab (in IE, Chrome you can get the console with F12) or if you are using FireFox then use FireBug or a thrid party tool like Fiddler.
Then you can see what went wrong on the server and go further to solve your problem.
In your case this is in the response:
"Message":"An error has occurred.","ExceptionMessage":"Property 'StartDate' on type 'MvcApplication3.Controllers.TaskViewModel' 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"
So your problem is not that you have two attributes but that you've marked your properties with
[Required]
to solve this the exception tells you what to do.You need to add
[DataMember(IsRequired=true)]
to your required properties where the property type is a value type (e.g int, datatime, etc.):So change your
TaskViewModel
to:[DataContract] public class TaskViewModel { //Default Constructor public TaskViewModel() { } [DataMember] public Guid TaskId { get; set; } [Required] [DataMember] [StringLength(10)] public string Description { get; set; } [Required] [DataMember(IsRequired = true)] [DataType(DataType.DateTime)] public System.DateTime StartDate { get; set; } [Required] [DataMember] public string Status { get; set; } [DataMember] public System.Guid ListID { get; set; } }
Some side notes:
- You need to reference the System.Runtime.Serialization dll in order to use the
DataMemberAttribute
- You need to mark your class with
[DataContract]
and you need to mark all of its properties with[DataMember]
not just the required ones.这篇关于使用模型绑定和验证问题asp.net的Web API属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!