0

是否可以使用 .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 格式的响应

谢谢。

4

1 回答 1

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();
    }
}
于 2013-03-06T02:23:53.240 回答