检查有关Serialization
的一些文档,我得到的印象是,如果使用DataContractSerializer
,则没有特定的设置以类似的方式序列化json和xml。
现在,我遇到了一个与普通问题相反的问题,我无法以xml格式获得任何回报。 使用下面的代码段,其中d是我要发回的数据
System.Net.Http.Formatting.MediaTypeFormatter formatter;
if (contentType.FirstOrDefault() == "text/xml" || contentType.FirstOrDefault() == "application/xml")
{
formatter = new System.Net.Http.Formatting.XmlMediaTypeFormatter();
(formatter as System.Net.Http.Formatting.XmlMediaTypeFormatter).Indent = true;
}
else if (contentType.FirstOrDefault() == "text/html")
{
formatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
}
else
{
formatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
(formatter as System.Net.Http.Formatting.JsonMediaTypeFormatter).SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
(formatter as System.Net.Http.Formatting.JsonMediaTypeFormatter).SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
}
httpResponseMessage = httpRequestMessage.CreateResponse(HttpStatusCode.OK, d, formatter);
d
定义为
public class FeatureModel : ModelBase
{
public List<Feature> Data { get; set; }
public class Feature : Common.ClassBase
{
/// <summary>
///
/// </summary>
public int feature_id { get; set; }
}
}
我可以在回复为空时以json格式接收数据