WCF 4.0 Rest Service设置内容类型 [英] WCF 4.0 Rest Service setting content type

查看:68
本文介绍了WCF 4.0 Rest Service设置内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了我的第一个WCF 4.0 Rest服务,并且不明白为什么在通过Fiddler和Firefox调用服务之间返回的数据的Content-Type会发生变化。这是我的服务合同:

I've just finished my first WCF 4.0 Rest service and don't understand why the Content-Type of the data being returned changes between calling the service via Fiddler and Firefox. Here's my service contract:

[ServiceContract]
public interface IProjectService
{
    [OperationContract]
    [WebGet(UriTemplate = "project/{id}/json", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    ProjectDataContract GetProjectJson(string id);

    [OperationContract]
    [WebGet(UriTemplate = "project/{id}/xml", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]
    ProjectDataContract GetProjectXml(string id);

    [OperationContract]
    [WebGet(UriTemplate = "userprojects/{userKey}/json", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    List<ProjectDataContract> GetProjectsByUserJson(string userKey);

    [OperationContract]
    [WebGet(UriTemplate = "userprojects/{userKey}/xml", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]
    List<ProjectDataContract> GetProjectsByUserXml(string userKey);
}

如您所见,我正在为每个操作设置响应格式。如果请求以 / json结尾,那么我将返回json数据。如果请求以 / xml结尾,则返回xml数据。至少那是我的意图。

As you can see, I'm setting the response format for each operation. If the request ends with "/json" then I'm returning json data. If the request ends with "/xml", then xml data is returned. At least that is what my intentions are.

当我打电话给 http:// localhost:5050 / ProjectServiceLibrary / project / 27 / xml 在Firefox中,我可以看到content-type设置为 text / html,而在提琴手显示正确的内容类型 application / xml。调用 / json后缀请求(firefox中的 text / html和提琴手中的 application / json)时也会发生同样的事情。

When I make a call to http://localhost:5050/ProjectServiceLibrary/project/27/xml in Firefox, I can see the content-type is set to "text/html" whereas the same request invoked in fiddler shows the correct content type of "application/xml". Same thing happens for a call to a "/json" suffixed request - "text/html" in firefox and "application/json" in fiddler.

为什么呢?这发生了吗?我信任哪一个?我下载了JSONView Firefox插件,但这使所有内容看起来都像json。它将XML视为JSON。

So, why is this happening? Which one do I trust? I downloaded JSONView Firefox add-on, but that makes everything look like json. It treats the XML as JSON.

我确定我缺少明显的东西。任何帮助将不胜感激。

I'm sure I'm missing something obvious. Any help would be greatly appreciated.

推荐答案

这与客户端发送的请求中的Accept标头有关。 Accept标头包含MIME类型的优先列表。接受标头由客户端(Firefox,Fiddler)定义,并告诉服务器它可以接收哪些内容类型。服务器将根据优先级和兼容性使用最佳匹配。

This is related to the Accept header in the request sent by the client. The Accept header contains a prioritized list of MIME types. Accept headers are defined by the client (Firefox, Fiddler), and tell the server which content-types it is capable of receiving. The server will use the best match based on priority and compatibility.

FireFox生成的接受标头赋予text / html更高的优先级-告诉服务器发送text / html有可能的。您可能会发现Fiddler的做法相反,将application / xml赋予更高的优先级-这解释了您所看到的。

Accept headers generated by FireFox give text/html a higher priority - telling the server to send text/html if it is possible. You will probably find that Fiddler does the opposite, giving application/xml the higher priority - this explains what you are seeing.

关于克里斯·乔丹斯博客。

这篇关于WCF 4.0 Rest Service设置内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆