REST API 仅设置对象的某些属性

发布于 2024-12-27 14:09:38 字数 2679 浏览 0 评论 0原文

我正在开发一个 REST API,我将其发布到使用 POST 和 XML 作为内容。在我的 WebInvoke 方法中,它似乎只序列化了一些属性。例如,我传递了名字、姓氏、电子邮件、电话和地址,但它没有设置电子邮件和地址属性。因此,当它将数据添加到我的数据库时,这些字段为空。

这是为什么呢?为什么它设置某些属性而不设置其他属性?我尝试过重新安排顺序,但没有任何影响。这也让我想到了另一个问题:所有属性都必须在 xml 中传递吗?还是只传递一些像我这里这样的属性就足够了?我希望答案是它不需要全部,因为这可能是一个非常动态的系统,并且可能会频繁添加新属性,而无需更改 xml。

以下是一些相关代码:

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...

这是 OperationContract

[OperationContract]
    [WebInvoke(Method = "POST",
        UriTemplate = "leads",
        BodyStyle= WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml)]
    string AddLead(Lead lead);

这是服务中的 AddLead 函数:

public string AddLead(Lead lead)
    {
        string result = lead.Submit();
        if (result == "Success")
        {
            return "Success. " + lead.LeadId;
        }
        else
        {
            return result;
        }
    }

这是我传递的 XML:

<?xml version="1.0" encoding="utf-8"?>
   <Lead xmlns="http://www.myrenamednamespace.com/leads">
      <FirstName>John</FirstName>
      <LastName>Doe</LastName>
      <Email>[email protected]</Email>
      <Phone>8885551234</Phone>
      <Address>123 Fake St</Address>
      <City>Fake City</City>
   </Lead>

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.

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.

Here is some of the relevant 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...

Here's the OperationContract

[OperationContract]
    [WebInvoke(Method = "POST",
        UriTemplate = "leads",
        BodyStyle= WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml)]
    string AddLead(Lead lead);

Here's the AddLead function in the Service:

public string AddLead(Lead lead)
    {
        string result = lead.Submit();
        if (result == "Success")
        {
            return "Success. " + lead.LeadId;
        }
        else
        {
            return result;
        }
    }

And here is the XML that I'm passing:

<?xml version="1.0" encoding="utf-8"?>
   <Lead xmlns="http://www.myrenamednamespace.com/leads">
      <FirstName>John</FirstName>
      <LastName>Doe</LastName>
      <Email>[email protected]</Email>
      <Phone>8885551234</Phone>
      <Address>123 Fake St</Address>
      <City>Fake City</City>
   </Lead>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千年*琉璃梦 2025-01-03 14:09:38

您需要在 DataMember 上指定顺序。 DataContractSerializer 假定按字母顺序排序,除非您另有说明,并且它会忽略它发现的不符合预期顺序的项目(这就是为什么它成功拨打电话但未成功发送电子邮件的原因)

public class Lead
{
    [DataMember(Name = "LeadId", Order=1)]
    public int LeadId { get; set; }

    [DataMember(Name = "FirstName", Order = 2)]
    public string FirstName { get; set; }

    [DataMember(Name = "MiddleName", Order = 3)]
     public string MiddleName { get; set; }

     // ... etc
}

You need to specify the order on the DataMember. The DataContractSerializer assumes alphabetic ordering unless you tell it otherwise and it ignores items it finds out of the expected order (which is why it did phone but not email successfully)

public class Lead
{
    [DataMember(Name = "LeadId", Order=1)]
    public int LeadId { get; set; }

    [DataMember(Name = "FirstName", Order = 2)]
    public string FirstName { get; set; }

    [DataMember(Name = "MiddleName", Order = 3)]
     public string MiddleName { get; set; }

     // ... etc
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文