Web服务始终返回混合的xml和json。如何使它成为纯粹的json

时间:2014-12-23 11:31:16

标签: c# asp.net json web-services

我正在开发一个项目,在这个项目中我被要求创建一个返回纯json的web服务

我制作了这段代码,但它总是返回混合的xml和json

namespace DotMeTast
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class NewsWebService : System.Web.Services.WebService
    {
        NewsDataContext _db = new NewsDataContext();


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false, XmlSerializeString = false)]
        public string getAllNews()
        {

            var news = (from p in _db.NewsTBs
                        where p.nType == "Real"
                        select p).ToList();
            var jsonSerialiser = new JavaScriptSerializer();
            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            var json = jsonSerialiser.Serialize(news);
            return json;
        }
    }
}

关于如何使它成为纯粹的json的任何线索

1 个答案:

答案 0 :(得分:0)

感谢利亚姆,我做了我的研究,发现比网络服务更好

我使用了web api,它正在使用correctlley

我找到的链接帮助我了解如何在asp.net web表单中实现web api

WebForms With Web Api