我在C#ASMX Web服务中有以下类,而不是MVC或Web表单项目.
public class Hotel
{
public int HotelId {get;set;}
public string Name {get;set;}
public Room[] Room { get; set; }
[Range(0, 12, ErrorMessage = "Rating must be between 1 and 5")]
public int Rating {get;set:}
}
public class Room
{
[Required]
public int RoomId {get;set;}
[Required]
[StringLength(175)]
public string Name {get; set;}
}
使用System.ComponentModel.DataAnnotations,因为我能像上面那样有效吗?如果是这样,我如何获得验证错误的响应?
此外,当服务启动时,我读入如下的Json数据对象.
oHotelRequest = JsonConvert.DeserializeObject<Hotel>(sJson);
HotelResponse oHotelResponse = BookingAPI.Hotel.Get(oHotelRequest);
return JsonConvert.SerializeObject(oHotelResponse);
或者我可以在反序列化对象时进行验证?
最佳答案 您可以查看此网页:
http://odetocode.com/blogs/scott/archive/2011/06/29/manual-validation-with-data-annotations.aspx