即使我使用属性标记方法,我也无法成功将JSON从WCF服务中获取:
[WebGet(UriTemplate = "Product/{productIdString}",
ResponseFormat = WebMessageFormat.Json)]
OR
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
我总是使用XML,无论是以DataSet
还是List<>
还是<。
唯一可行的方法是手动将JSON作为字符串返回,但它也封装在XML中。
有任何线索吗?
答案 0 :(得分:2)
尝试这样的事情:
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "products")]
public IList<JxProduct> GetProductList()
{
List<JxProduct> products = new List<JxProduct>();
products.Add(new JxProduct { Description = "Tire", Id = 1, Price = 39.99});
products.Add(new JxProduct { Description = "Tube", Id = 2, Price = 4.99 });
products.Add(new JxProduct { Description = "Patch", Id = 3, Price = 3.99});
return products;
}
您还可以查看以下帖子,其中详细介绍了有关web.config设置的信息。 How do I return clean JSON from a WCF Service?
答案 1 :(得分:0)
[WebGet]
(和[WebInvoke]
)属性仅由WebHttpBehavior
(<webHttp/>
识别,如果您使用的是配置)。确保您正在点击的端点具有该行为设置。