我在web api中有一个POST方法,它有一个RequestAppointment参数。例如:
// POST: api/RequestAppointment
public HttpResponseMessage Post([FromBody]RequestAppointment httpRequest)
{
var response = Appointment.CreateAppointment(httpRequest);
return Request.CreateResponse<ResponseRequest>(System.Net.HttpStatusCode.Created, response);
}
RequestAppoint类具有以下结构:
public partial class RequestAppointment
{
public RequestAppointmentAppointmentInformation appointmentInformation { get; set; }
}
public partial class RequestAppointmentAppointmentInformation
{
public string AppointmentCcEmail { get; set; }
public string ClaimType { get; set; }
public RequestAppointmentAppointmentInformationClaimant Claimant { get; set; }
}
public partial class RequestAppointmentAppointmentInformationClaimant
{
public System.DateTime DateOfBirth { get; set; }
}
可以提出两个请求: 第一个是:
<RequestAppointment>
<appointmentInformation>
<AppointmentCcEmail>x.x.com</AppointmentCcEmail>
<ClaimType>A</ClaimType>
<Claimant>
<DateOfBirth>1961-12-25</DateOfBirth>
</Claimant>
</appointmentInformation>
</RequestAppointment>
第二个请求是:
<RequestAppointment>
<appointmentInformation>
<AppointmentCcEmail>x.x.com</AppointmentCcEmail>
<ClaimType>A</ClaimType>
</appointmentInformation>
</RequestAppointment>
两个请求XML之间的区别在于#1具有Claimant标记,而#2没有。
第二个请求没有,因为当用户从数据接口调用Web服务时未提供请求者。虽然第一个提供Claimant时总会有。
在我使用Fiddler的测试中,我的#1请求返回数据。也就是说,它能够反序列化为对象RequestAppointment,而#2请求返回空值。
我该怎么做才能解决这个问题?请求将始终采用这两种格式,并且它们将始终使用相同的模型结构。
我该怎么做才能解决这个问题?我在Fiddler中测试了这个场景。
答案 0 :(得分:0)
首先,步骤1.如果您有RequestAppointment
XSD文件,最好的解决方案是使用xsd.exe see haw to use it生成类,否则您可以自己创建XSD并执行步骤1,或者最后你应该进行更改并指出显式属性,使你的类XML可序列化。有很多关于它的文章。
使用xsd.exe生成此类,我使用XML创建示例模式,仅在标记<Claimant>
中进行更改,使其成为可选项,类看起来像这样。注意工具如何生成可选字段Claimant
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.6.1055.0.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class RequestAppointment {
private RequestAppointmentAppointmentInformation appointmentInformationField;
/// <remarks/>
public RequestAppointmentAppointmentInformation appointmentInformation {
get {
return this.appointmentInformationField;
}
set {
this.appointmentInformationField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class RequestAppointmentAppointmentInformation {
private string appointmentCcEmailField;
private string claimTypeField;
private RequestAppointmentAppointmentInformationClaimant claimantField;
/// <remarks/>
public string AppointmentCcEmail {
get {
return this.appointmentCcEmailField;
}
set {
this.appointmentCcEmailField = value;
}
}
/// <remarks/>
public string ClaimType {
get {
return this.claimTypeField;
}
set {
this.claimTypeField = value;
}
}
/// <remarks/>
public RequestAppointmentAppointmentInformationClaimant Claimant {
get {
return this.claimantField;
}
set {
this.claimantField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class RequestAppointmentAppointmentInformationClaimant {
private System.DateTime dateOfBirthField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="date")]
public System.DateTime DateOfBirth {
get {
return this.dateOfBirthField;
}
set {
this.dateOfBirthField = value;
}
}
}
XSD示例
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="RequestAppointment">
<xs:complexType>
<xs:sequence>
<xs:element name="appointmentInformation">
<xs:complexType>
<xs:sequence>
<xs:element name="AppointmentCcEmail" type="xs:string" />
<xs:element name="ClaimType" type="xs:string" />
<xs:element name="Claimant" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="DateOfBirth" type="xs:date" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
答案 1 :(得分:0)
感谢您的所有建议。他们都是伟大的! Seyran的答案确实很棒。但就我的问题而言,我通过nil =&#34; true&#34;解决了这个问题。 (nullable = true)所有元素。我使用xsd为我的请求xml生成类。
由于