当前位置: 动力学知识库 > 问答 > 编程问答 >

WCF response is returned as text/html, not xml

问题描述:

I think something must not be correct with the serialization. My class is:

[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;

}

}

The API call:

 [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);

}

The generated source:

<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>

It doesn't display the xml on the page because it says the response type is text/html. Any ideas?

网友答案:

What kind of client are you using to invoke the web service?

You could try

WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
网友答案:

Have you tried adding the ResponseFormat = WebMessageFormat.Xml?

[WebGet(UriTemplate = "?promotion_id={promotion_id}&page={page}&format={format}", ResponseFormat = WebMessageFormat.Xml)]
网友答案:

You are setting the outgoing response format to be Json when you pass the format value as json.

WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;

Have you made sure about sending the proper format parameter. Also try to inspect the request and response using Fiddler.

分享给朋友:
您可能感兴趣的文章:
随机阅读: