Note that there are some explanatory texts on larger screens.

plurals
  1. POREST API only sets certain properties of object
    text
    copied!<p>I am working on a REST API that I post to using POST and XML as the content. In my WebInvoke method, it seems to only be serializing a few of the properties. For example, I pass FirstName, LastName, Email, Phone, and Address, but it isn't setting the Email and Address properties. So when it adds the data to my database, those fields are blank.</p> <p>Why is this? Why is it setting some of the properties but not others? I've tried re-arranging the order and that didn't affect anything. That also brings me to one other question: Do ALL the properties have to be passed in the xml, or will only passing a few like I have here suffice? I hope the answer is it doesn't need all, because this may be a pretty dynamic system and new properties may be added frequently without the xml being changed.</p> <p>Here is some of the relevant code:</p> <pre class="lang-cs prettyprint-override"><code>public class Lead { #region Public Properties [DataMember(Name = "LeadId")] public int LeadId { get; set; } [DataMember(Name="FirstName")] public string FirstName { get; set; } [DataMember(Name = "MiddleName")] public string MiddleName { get; set; } [DataMember(Name = "LastName")] public string LastName { get; set; } [DataMember(Name = "Email")] public string Email { get; set; } [DataMember(Name = "Email2")] public string Email2 { get; set; } [DataMember(Name = "Phone")] public string Phone { get; set; } [DataMember(Name = "Phone2")] public string Phone2 { get; set; } [DataMember(Name = "Address")] public string Address { get; set; } [DataMember(Name = "Address2")] public string Address2 { get; set; } [DataMember(Name = "Address3")] public string Address3 { get; set; } [DataMember(Name = "City")] public string City { get; set; } [DataMember(Name = "State")] public string State { get; set; } [DataMember(Name = "Zip")] etc... </code></pre> <p>Here's the OperationContract</p> <pre class="lang-cs prettyprint-override"><code>[OperationContract] [WebInvoke(Method = "POST", UriTemplate = "leads", BodyStyle= WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] string AddLead(Lead lead); </code></pre> <p>Here's the AddLead function in the Service:</p> <pre class="lang-cs prettyprint-override"><code>public string AddLead(Lead lead) { string result = lead.Submit(); if (result == "Success") { return "Success. " + lead.LeadId; } else { return result; } } </code></pre> <p>And here is the XML that I'm passing:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Lead xmlns="http://www.myrenamednamespace.com/leads"&gt; &lt;FirstName&gt;John&lt;/FirstName&gt; &lt;LastName&gt;Doe&lt;/LastName&gt; &lt;Email&gt;JohnDoe@gmail.com&lt;/Email&gt; &lt;Phone&gt;8885551234&lt;/Phone&gt; &lt;Address&gt;123 Fake St&lt;/Address&gt; &lt;City&gt;Fake City&lt;/City&gt; &lt;/Lead&gt; </code></pre>
 

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