c# - 如何验证存储在 C# 类中的数据

标签 c# validation asmx

我在 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);

或者我可以在反序列化对象时进行验证吗?

最佳答案

关于c# - 如何验证存储在 C# 类中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17921919/

相关文章:

web-services - Web 服务是否应该抛出异常或结果对象

javascript - 当时对可见字段进行表单验证?

java - 在 GWT 中使用 javax.validation 会引发运行时错误 ClassNotFoundException

c# - 使用 C# 函数检查空文本框

c# - 如何通过 GET 调用 ASMX 网络服务?

c# - 如何使用正则表达式将前面没有特殊字符的两个大写字母匹配在一起?

c# - 如何根据 asp.net 中的分辨率以编程方式创建填充客户端浏览器的按钮列表

c# - 如何判断 IIS 何时在 ASP.NET 中应用 "Default Page"设置 (C#)

c# - 为 ComboBox 设置默认值

asmx - 何时使用页面方法与创建 Web 服务?