【发布时间】: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;
}
【问题讨论】:
-
尝试使用
[FromBody]属性,看看是否能解决问题。Go([FromBody] ParameterType request) -
显示课程。需要能够将 json 与其映射到的类进行比较
-
好吧,我想我明白了。你的 json 有一层太深,基本上有
new { createCustomerPaymentProfileRequest = new {...}}当你应该将该属性作为根时
标签: c# asp.net asp.net-web-api visual-studio-2017 authorize.net