WCF ResponseFormat基于查询字符串

时间:2013-03-06 02:15:56

标签: .net wcf

是否可以使用.NET 4.0框架根据查询字符串值更改WCF Rest响应的格式?我想基于查询字符串值发送XML和JSON响应。

[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
    }
}

例如: www.mysite.com/list?format=xml将返回XML格式的响应和 www.mysite.com/list?format=json将返回JSON格式的响应

感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用Format对象的WebOperationContext.Current.OutgoingResponse属性来完成您想要的任务。请参阅以下代码中的示例。

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();
    }
}