.net 基于查询字符串的WCF响应格式
是否可以使用.NET 4.0 framework基于querystring值更改WCF Rest响应的格式?我想基于querystring值发送XML和JSON响应.net 基于查询字符串的WCF响应格式,.net,wcf,.net,Wcf,是否可以使用.NET 4.0 framework基于querystring值更改WCF Rest响应的格式?我想基于querystring值发送XML和JSON响应 [OperationContract] [WebGet(UriTemplate = "/list")] List<SomeObject> List(); [DataContract] public class SomeObject { private int id; private string val
[OperationContract]
[WebGet(UriTemplate = "/list")]
List<SomeObject> List();
[DataContract]
public class SomeObject
{
private int id;
private string value;
private SomeObject()
{ }
private SomeObject(int id, string value)
{
this.id = id;
this.value= value;
}
[DataMember(Order = 0)]
public int Id
{
get { return id; }
set { }
}
[DataMember(Order = 1)]
public string Value
{
get { return value; }
set { }
}
public static List<SomeObject> List()
{
// return a list of SomeObject
}
}
[运营合同]
[WebGet(UriTemplate=“/list”)]
List();
[数据合同]
公共类对象
{
私有int-id;
私有字符串值;
私有对象()
{ }
私有SomeObject(int id,字符串值)
{
this.id=id;
这个。值=值;
}
[数据成员(订单=0)]
公共整数Id
{
获取{return id;}
集合{}
}
[数据成员(顺序=1)]
公共字符串值
{
获取{返回值;}
集合{}
}
公共静态列表()
{
//返回某个对象的列表
}
}
例如:
www.mysite.com/list?format=xml将返回xml格式的响应,然后
www.mysite.com/list?format=json将返回json格式的响应
谢谢。您可以使用
WebOperationContext.Current.OutgoingResponse
对象的Format
属性来完成您想要的操作。请参见下面代码中的示例
public class StackOverflow_15237791
{
[DataContract]
public class SomeObject
{
private int id;
private string value;
private SomeObject()
{ }
public SomeObject(int id, string value)
{
this.id = id;
this.value = value;
}
[DataMember(Order = 0)]
public int Id
{
get { return id; }
set { }
}
[DataMember(Order = 1)]
public string Value
{
get { return value; }
set { }
}
}
[ServiceContract]
public class Service
{
[WebGet(UriTemplate = "/list?format={format}")]
public List<SomeObject> List(string format)
{
if ("xml".Equals(format, StringComparison.OrdinalIgnoreCase))
{
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Xml;
}
else if ("json".Equals(format, StringComparison.OrdinalIgnoreCase))
{
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
}
else
{
throw new WebFaultException<string>("Format query string parameter required", HttpStatusCode.BadRequest);
}
return new List<SomeObject>
{
new SomeObject(1, "hello"),
new SomeObject(2, "world")
};
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
host.Open();
Console.WriteLine("Host opened");
WebClient c = new WebClient();
Console.WriteLine(c.DownloadString(baseAddress + "/list?format=xml"));
c = new WebClient();
Console.WriteLine(c.DownloadString(baseAddress + "/list?format=json"));
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
公共类堆栈溢出\u 15237791
{
[数据合同]
公共类对象
{
私有int-id;
私有字符串值;
私有对象()
{ }
公共SomeObject(int id,字符串值)
{
this.id=id;
这个值=值;
}
[数据成员(订单=0)]
公共整数Id
{
获取{return id;}
集合{}
}
[数据成员(顺序=1)]
公共字符串值
{
获取{返回值;}
集合{}
}
}
[服务合同]
公务舱服务
{
[WebGet(UriTemplate=“/list?format={format}”)]
公共列表(字符串格式)
{
if(“xml”.Equals(格式,StringComparison.OrdinalIgnoreCase))
{
WebOperationContext.Current.OutgoingResponse.Format=WebMessageFormat.Xml;
}
else if(“json”.Equals(格式,StringComparison.OrdinalIgnoreCase))
{
WebOperationContext.Current.OutgoingResponse.Format=WebMessageFormat.Json;
}
其他的
{
抛出新的WebFaultException(“需要格式化查询字符串参数”,HttpStatusCode.BadRequest);
}
返回新列表
{
新建SomeObject(1,“你好”),
新SomeObject(2,“世界”)
};
}
}
公共静态无效测试()
{
string baseAddress=“http://“+Environment.MachineName+”:8000/服务”;
WebServiceHost主机=新的WebServiceHost(类型(服务),新的Uri(基地址));
host.Open();
Console.WriteLine(“主机已打开”);
WebClient c=新的WebClient();
WriteLine(c.DownloadString(baseAddress+“/list?format=xml”);
c=新的WebClient();
WriteLine(c.DownloadString(baseAddress+“/list?format=json”);
控制台。写入(“按ENTER键关闭主机”);
Console.ReadLine();
host.Close();
}
}