Note that there are some explanatory texts on larger screens.

plurals
  1. POPutAsJsonAsync does not work when dto has data annotations
    primarykey
    data
    text
    <p>I'm seeing some strange behavior with HttpClient and Web API with my DTOs. When I have data annotations in place for my properties, HttpClient.PutAsJsonAsync() does not work. I can't receive anything at the Web API end. Some code to explain:</p> <p>My MVC 4 web page calls the Web API with this code:</p> <pre><code>using (var client = new HttpClient()) { var response = client.PutAsJsonAsync(uri+"/"+MyObject.Id, MyObject).Result; response.EnsureSuccessStatusCode(); // Returns 500 when i use MyObject with annotations } </code></pre> <p>Web API controller code to receive. Note that this is not even trigged when MyObject has annotations:</p> <pre><code>public MyObject Put(MyObject myObject) { try { if (myObject == null) throw new NullReferenceException(); } catch (Exception e) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest)); } } </code></pre> <p>MyObject DTO when it works:</p> <pre><code>public class MyObject { public int Id { get; set; } public Nullable&lt;int&gt; AuditProgramId { get; set; } public string Title { get; set; } public System.DateTime StartDate { get; set; } public System.DateTime EndDate { get; set; } } </code></pre> <p>MyObject DTO when it doesn't work:</p> <pre><code>public class MyObject { public int Id { get; set; } public Nullable&lt;int&gt; 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; } } </code></pre> <p>Any ideas?</p> <p><strong>UPDATE 1</strong></p> <p>It works with these values without annotations, but fails with annotations:</p> <pre><code>var myObj = new MyObject { Id=4, Title="Test Title", StartDate=DateTime.Today, EndDate=DateTime.Today.AddDays(2) }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload