我有一些几乎相同的 WCF 服务代码,除了一个方法应该返回一个 xml 结果,另一个是一个 json 结果:
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "xml/{id}")]
string XMLData(string id);
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json/{id}")]
string JSONData(string id);
}
xml 工作正常(例如,当我在浏览器中输入“ http://localhost:4841/RestServiceImpl.svc/xml/2468 ”时)。
但是,当我输入“ http://localhost:4841/RestServiceImpl.svc/json/2468 ”时,我会看到一个“文件下载 - 安全警告”对话框,它允许我保存一个文件(在这种情况下名为“2468”) ,在记事本中打开时包含以下内容:
{"JSONDataResult":"您请求的产品 2468"}
这是“按设计”(将 json 结果保存到文件),还是为什么它的行为方式与 xml-o-rama 不同?