简体   繁体   English

当dto具有数据注释时,PutAsJsonAsync不起作用

[英]PutAsJsonAsync does not work when dto has data annotations

I'm seeing some strange behavior with HttpClient and Web API with my DTOs. 我看到HttpClient和Web API与我的DTO有一些奇怪的行为。 When I have data annotations in place for my properties, HttpClient.PutAsJsonAsync() does not work. 当我为我的属性准备好数据注释时,HttpClient.PutAsJsonAsync()不起作用。 I can't receive anything at the Web API end. 我无法在Web API端收到任何内容。 Some code to explain: 一些代码解释:

My MVC 4 web page calls the Web API with this code: 我的MVC 4网页使用以下代码调用Web API:

using (var client = new HttpClient())
{
    var response = client.PutAsJsonAsync(uri+"/"+MyObject.Id, MyObject).Result;
    response.EnsureSuccessStatusCode(); // Returns 500 when i use MyObject with annotations                             
}

Web API controller code to receive. 要接收的Web API控制器代码。 Note that this is not even trigged when MyObject has annotations: 请注意,当MyObject具有注释时,甚至不会触发此操作:

public MyObject Put(MyObject myObject)
{
        try
        {
            if (myObject == null) throw new NullReferenceException();
        }
        catch (Exception e)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest));
        }
}

MyObject DTO when it works: MyObject DTO工作时:

public class MyObject
{
    public int Id { get; set; }
    public Nullable<int> AuditProgramId { get; set; }
    public string Title { get; set; }
    public System.DateTime StartDate { get; set; }
    public System.DateTime EndDate { get; set; }
 }

MyObject DTO when it doesn't work: MyObject DTO不起作用时:

public class MyObject
{
    public int Id { get; set; }
    public Nullable<int> AuditProgramId { get; set; }
    [Required]
    public string Title { get; set; }
    [Required, DataType(DataType.Date)]
    public System.DateTime StartDate { get; set; }
    [Required, DataType(DataType.Date)]
    public System.DateTime EndDate { get; set; }
 }

Any ideas? 有任何想法吗?

UPDATE 1 更新1

It works with these values without annotations, but fails with annotations: 它可以在没有注释的情况下使用这些值,但是注释失败:

var myObj = new MyObject {
    Id=4,
    Title="Test Title",
    StartDate=DateTime.Today,
    EndDate=DateTime.Today.AddDays(2)
};

I can repro your scenario, and the exception message actually provide a solution to this problem: 我可以重新编写您的方案,并且异常消息实际上提供了此问题的解决方案:

Property 'StartDate' on type 'MvcApplication.Model.MyObject' 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)].

I have modified my MyObject class accordingly and I got your scenario to work. 我相应地修改了我的MyObject类,让你的场景工作。

[DataContract]
public class MyObject
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public Nullable<int> AuditProgramId { get; set; }

    [DataMember]
    [Required]
    public string Title { get; set; }

    [Required, DataType(DataType.Date)]
    [DataMember(IsRequired = true)]
    public System.DateTime StartDate { get; set; }

    [Required, DataType(DataType.Date)]
    [DataMember(IsRequired = true)]
    public System.DateTime EndDate { get; set; }
}

FYI, a bug relating to this scenario is recently fixed to make things simpler: Overly aggressive validation for applying [DataMember(IsRequired=true)] to required properties with value types 仅供参考,最近修复了与此场景相关的错误,以简化操作: 将[DataMember(IsRequired = true)]应用于具有值类型的必需属性的过于激进的验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM