1. 程式人生 > >WCF輸出JSON

WCF輸出JSON

msg for format) 代碼 current text == 判斷 tex

    public class MyService : IService
    {
        public Message GetXml(string format)
        {
            WebOperationContext context = WebOperationContext.Current;

            Book b = new Book
            {
                BookName = "賣女孩的小火柴",
                Price = 25.2M,
                BarCode = "2811365801"
            };

            Message msgreturn = null;
            // 判斷格式
            if (format.ToLower() == "xml")
            {
                msgreturn = context.CreateXmlResponse<Book>(b);
            }
            else
            {
                msgreturn = context.CreateJsonResponse<Book>(b);
            }

            return msgreturn;
        }
    }
技術分享

WCF輸出JSON