控制器
public class TestController : ApiController
{
public IHttpActionResult GetDepartment()
{
Department department = new Department();
department.Code = "123456";
return Ok(department);
}
{
模型
[Serializable]
[DataContract]
public class Department
{
public Department()
{
this.Employees = new List<Employee>();
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[DataMember(Name = "id")]
public int Id { get; set; }
[DataMember(Name = "name", IsRequired = true)]
public string Name { get; set; }
[RegularExpression(@"^(?!00000)[0-9]{5,5}$", ErrorMessage = "Invalid input for field Code")]
[MaxLength(5)]
public string Code { get; set; }
[DataMember(Name = "employees", IsRequired = false)]
public List<Employee> Employees { get; set; }
}
我想创建一个自定义jsonconverter,它将序列化以下内容,但我不知道该怎么做。
此外,数据注释未按预期工作似乎很奇怪。有没有其他方法可以执行数据注释?
顺便说一句,我正在使用ASPNET WEBAPI DOTNET 4.5。