我认为序列化一定不能正确。我的班级是:
[DataContract]
public class User
{
[DataMember]
public int id { get; set; }
[DataMember]
public string user_id { get; set; }
...
public User() { }
public User(int id, string user_id, ...)
{
this.id = id;
this.user_id = user_id;
...
}
}
[DataContract]
public class UserCollection
{
[DataMember]
public List<User> users { get; set; }
[DataMember]
public int count { get; set; }
[DataMember]
public int page { get; set; }
public UserCollection() { }
public UserCollection(List<User> users, int count, int page)
{
this.users = users;
this.count = count;
this.page = page;
}
}
API调用:
[WebGet(UriTemplate = "?promotion_id={promotion_id}&page={page}&format={format}")]
public UserCollection GetAllUsers(string promotion_id, string page, string format)
{
if (string.Equals("json", format, StringComparison.OrdinalIgnoreCase))
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
UserFactory factory = new UserFactory();
return factory.GetUsersByPromotionID(int.Parse(promotion_id), (int.Parse(page) - 1) * 50, 50);
}
生成的来源:
<UserCollection xmlns="http://schemas.datacontract.org/2004/07/API.Library.Resources" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><count>0</count><page>0</page><users/></UserCollection>
它不会在页面上显示xml,因为它表示响应类型为text / html。有什么想法吗?
答案 0 :(得分:0)
您使用什么类型的客户端来调用Web服务?
你可以尝试
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
答案 1 :(得分:0)
您是否尝试过添加ResponseFormat = WebMessageFormat.Xml
?
[WebGet(UriTemplate = "?promotion_id={promotion_id}&page={page}&format={format}", ResponseFormat = WebMessageFormat.Xml)]
答案 2 :(得分:0)
当您将格式值作为json传递时,您将传出响应格式设置为Json。
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
您确定发送了正确的格式参数吗?还尝试使用Fiddler检查请求和响应。