Sto ricevendo il seguente VM su un Web API Messaggio azioneWeb API annullabile richiesto richiede attributo DataMember
public class ViewModel
{
public string Name { get; set; }
[Required]
public int? Street { get; set; }
}
Quando faccio un post ottengo il seguente errore:
Property 'Street' on type 'ViewModel' 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)].
Sembra l'errore è chiaro, quindi voglio essere completamente sicuro che sia necessario utilizzare gli attributi [DataContract] e [DataMember] quando hai una classe con le proprietà nullable richieste.
C'è un modo per evitare di utilizzare questi attributi nell'API Web?
è passato un po 'di tempo da quando ho postato questa domanda e in realtà ho finito per fare quello che suggerisci, non capisco ancora perché web api funzioni in questo modo – jorgehmv
@jorgehmv Lo considero un bug, fino a quando qualcuno non mi convince diversamente. – aknuds1
Questo non è più un problema nella versione WebApi 2. Il bug sembra essere risolto. Semmai sono infastidito dal fatto che devo rendere gli attributi ViewModel nullable quando sto sottostando valori per Richiesto per restituire il messaggio di errore corretto. –