【问题标题】:why is concrete object being set to null in controller method?为什么在控制器方法中将具体对象设置为 null?
【发布时间】:2018-01-22 22:14:50
【问题描述】:

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

我发送的请求如下:

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

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

为什么我的请求的属性被设置为 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;
}

【问题讨论】:

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


【解决方案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; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 2014-10-27
    • 2013-03-21
    • 1970-01-01
    相关资源
    最近更新 更多