4

MVC または Web フォーム プロジェクトではなく、C# ASMX 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);

または、オブジェクトを逆シリアル化するときに検証を実行できますか?

4

1 に答える 1

2

次の Web ページをご覧ください: http://odetocode.com/blogs/scott/archive/2011/06/29/manual-validation-with-data-annotations.aspx

于 2013-07-29T10:54:26.403 に答える