WCF在ServiceContract中的WebGet批注中为ResponseFormat属性提供了两个选项.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "greet/{value}", BodyStyle = WebMessageBodyStyle.Bare)]
string GetData(string value);
[OperationContract]
[WebGet(UriTemplate = "foo", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
string Foo();
Run Code Online (Sandbox Code Playgroud)
ResponseFormat的选项是WebMessageFormat.Json和WebMessageFormat.Xml.是否可以编写自己的网络信息格式?我希望当客户端调用foo()方法时,他会获得原始字符串 - 没有json或xml包装器.
我有一个WCF restul服务,我想让用户选择他们想要的请求格式,我有装饰
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getstreamurl?ch={ch}&format=xml")]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getstreamurl?ch={ch}&format=json")]
Run Code Online (Sandbox Code Playgroud)
首先,有没有办法在运行时指定ResponseFormat并将格式作为方法的参数?从阅读周围我不这么认为......好的下一件事上面的代码是好的并且有效,但我有问题,我希望能够指定一个默认值,所以当没有格式争论传递然后我只是默认但如果我这样装饰
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getstreamurl?ch={ch})]
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getstreamurl?ch={ch}&format=json")]
Run Code Online (Sandbox Code Playgroud)
在XML是默认值的情况下,如果我尝试通过浏览器调用服务方法,它会告诉我:
UriTemplateTable不支持具有与模板'getstreamurl?ch = {ch}'等效路径的多个模板,但具有不同的查询字符串,其中查询字符串不能通过文字值消除歧义.有关更多详细信息,请参阅UriTemplateTable的文档
它们显然可以区分,但似乎WCF只是阅读论证并认为它...任何建议?