0

I am working on a project and in this project i am asked to make a webservice that returns a pure json

i made this code but it always returns mixed xml and 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;
        }
    }
}

Any Clue on how to make it pure json

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 2
    Web services use SOAP, SOAP **is** XML. I think you want a Web.API. – Liam Dec 23 '14 at 11:33
  • u mean i should do another project web api ??? – Abdallah Yehia Dec 23 '14 at 11:39
  • 1
    Web services typically use [SOAP](http://en.wikipedia.org/wiki/SOAP) as a communication method (can depend if your using WCF and dependant on your confirguration method, etc. but typically this is true). [Web.API](http://www.asp.net/web-api) supports flat json (without SOAP). SOAP is XML. If you don't want the SOAP wrapper you need to change the communication protocol to not use SOAP any more. – Liam Dec 23 '14 at 11:43
  • I am sorry is there a tutorial i should follow to do what i need . cuz i don't know what i should do now . or can u tell me simple steps to do . – Abdallah Yehia Dec 23 '14 at 11:49

1 Answers1

0

Thanks to Liam i did my research and found away better than webservice

i used web api and it's working correctlley

the link i found that helped me alot on how to implement web api in asp.net web form

WebForms With Web Api