WCF 4.0 Support Services Customization Content Type

I just finished my first WCF 4.0 Rest service and don’t understand why the Content-Type of the returned data changes between calling the service through Fiddler and Firefox. Here is 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); } 

As you can see, I am adjusting the response format for each operation. If the request ends with "/ json", I return json data. If the request ends with "/ xml", then the XML data is returned. At least these are my intentions.

When I make a call to http: // localhost: 5050 / ProjectServiceLibrary / project / 27 / xml in Firefox, I see that the content type is set to "text / html", while the same request that is called in fiddler shows the correct type content "application / xml". The same thing happens when the suffix "/ json" is called - "text / html" in firefox and "application / json" in the violin.

So why is this happening? Who do I trust? I downloaded the Firefox JSONView add-in, but it all looks like json. It treats XML as JSON.

I am sure that I am missing something obvious. Any help would be greatly appreciated.

+4
source share
1 answer

This is due to the Accept header in the request sent by the client. The Accept header contains a list of MIME priorities. Accept headers are determined by the client (Firefox, Fiddler) and tell the server what types of content it is able to receive. The server will use the best match based on priority and compatibility.

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

More information on Kris Jordans blog request headers.

+3
source

Source: https://habr.com/ru/post/1337387/


All Articles