为什么在控制器方法中将具体对象设置为空?

why is concrete object being set to null in controller method?

当我向我的控制器发送请求时,出于某种原因,它将所有空值分配给对象:

我发送的请求如下:

    {
  "createCustomerPaymentProfileRequest": {
      "merchantAuthentication": {
    "name": "3efsw3sd66",
    "transactionKey": "7m444433G"
  },
    "customerProfileId": "10000",
    "paymentProfile": {
      "billTo": {
        "firstName": "John",
        "lastName": "Doe",
        "address": "123 Main St.",
        "city": "Bellevue",
        "state": "WA",
        "zip": "98004",
        "country": "USA",
        "phoneNumber": "000-000-0000"
      },
      "payment": {
        "creditCard": {
          "cardNumber": "4111111111111111",
          "expirationDate": "2023-12"
        }
      },
      "defaultPaymentProfile": false
    },
    "validationMode": "liveMode"
  }
}

这是我发出邮递员请求的方式:

为什么我请求的属性设置为空?

class定义为:

 /// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="AnetApi/xml/v1/schema/AnetApiSchema.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="AnetApi/xml/v1/schema/AnetApiSchema.xsd", IsNullable=false)]
public partial class createCustomerPaymentProfileRequest : ANetApiRequest {

    /// <remarks/>
    public string customerProfileId;

    /// <remarks/>
    public customerPaymentProfileType paymentProfile;

    /// <remarks/>
    public validationModeEnum validationMode;

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool validationModeSpecified;
}

其父项定义为:

    /// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="AnetApi/xml/v1/schema/AnetApiSchema.xsd")]
public partial class ANetApiRequest {

    /// <remarks/>
    public merchantAuthenticationType merchantAuthentication;

    /// <remarks/>
    public string clientId;

    /// <remarks/>
    public string refId;
}

其中一个属性定义为:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="AnetApi/xml/v1/schema/AnetApiSchema.xsd")]
public partial class merchantAuthenticationType {

    /// <remarks/>
    public string name;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("accessToken", typeof(string))]
    [System.Xml.Serialization.XmlElementAttribute("clientKey", typeof(string))]
    [System.Xml.Serialization.XmlElementAttribute("fingerPrint", typeof(fingerPrintType))]
    [System.Xml.Serialization.XmlElementAttribute("impersonationAuthentication", typeof(impersonationAuthenticationType))]
    [System.Xml.Serialization.XmlElementAttribute("password", typeof(string))]
    [System.Xml.Serialization.XmlElementAttribute("sessionToken", typeof(string))]
    [System.Xml.Serialization.XmlElementAttribute("transactionKey", typeof(string))]
    [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
    public object Item;

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public ItemChoiceType ItemElementName;

    /// <remarks/>
    public string mobileDeviceId;
}

还有一个:

    public partial class customerPaymentProfileType : customerPaymentProfileBaseType {

    /// <remarks/>
    public paymentType payment;

    /// <remarks/>
    public driversLicenseType driversLicense;

    /// <remarks/>
    public string taxId;

    /// <remarks/>
    public bool defaultPaymentProfile;

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool defaultPaymentProfileSpecified;
}

正在发送的 json 级别太深了。

负载应该是

  {
    "merchantAuthentication": {
      "name": "3efsw3sd66",
      "transactionKey": "7m444433G"
    },
    "customerProfileId": "10000",
    "paymentProfile": {
      "billTo": {
        "firstName": "John",
        "lastName": "Doe",
        "address": "123 Main St.",
        "city": "Bellevue",
        "state": "WA",
        "zip": "98004",
        "country": "USA",
        "phoneNumber": "000-000-0000"
      },
      "payment": {
        "creditCard": {
          "cardNumber": "4111111111111111",
          "expirationDate": "2023-12"
        }
      },
      "defaultPaymentProfile": false
    },
    "validationMode": "liveMode"
  }

否则,为了与 json 匹配,模型必须看起来像这样

public class Example {
    public createCustomerPaymentProfileRequest createCustomerPaymentProfileRequest { get; set; }
}