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

时间:2017-08-15 01:26:09

标签: c# asp.net asp.net-web-api visual-studio-2017 authorize.net

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

enter image description here

我发送的请求如下:

    {
  "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"
  }
}

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

enter image description here

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

该类定义为:

 /// <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;
}

1 个答案:

答案 0 :(得分:1)

发送的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; }
}