Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am working on a ASP.NET Web Forms application using .net framework 4.0


I want to use web services in my application which will
-> be accessible from a browser (by typing in the URL)
-> return JSON


In my web config file, I have already added

XML
<webServices>
  <protocols>
    <add name="HttpGet"/>
  </protocols>
</webServices>



Here is what my asmx service looks like

[WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public List<Department> BindDepartments(int faculty = 0, string university = "", string branch = "")
        {
            List<Department> departmentList = new List<Department>();
            DepartmentBL departmentBL = new DepartmentBL();

            try
            {
                

                JavaScriptSerializer js = new JavaScriptSerializer();
                Context.Response.Clear();
                Context.Response.ContentType = "application/text";
                Context.Response.Write(js.Serialize(departmentBL.getDepartmentsByFaculty(faculty, university, branch)));

            } 
            catch (Exception ex)
            {

            }
            finally
            {
                departmentBL = null;
            }


            return departmentList;
        }



Using this, when I call my service from the browser, it returns a mix of XML and JSON

[{"DepartmentId":2,"DepartmentName":"Computer Science","DepartmentShortName":"CS","FacultyId":3,"FacultyName":"Science","Status":false,"CreatedBy":0,"CreatedTime":"\/Date(1406194645653)\/","ModifiedBy":0,"ModifiedTime":"\/Date(-62135596800000)\/"}]<?xml version="1.0" encoding="utf-8"?>
<ArrayOfDepartment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/" />




If I do not serialize and just return my C# List object, then I get pure XML data.

Please help me out here. What am I doing wrong?
Posted

1 solution

You write json result to response and return list collection again.
How about trying like this?
C#
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string BindDepartments(int faculty = 0, string university = "", string branch = "")
{
    List<department> departmentList = new List<department>();
    DepartmentBL departmentBL = new DepartmentBL();

    try
    {
        

        JavaScriptSerializer js = new JavaScriptSerializer();
        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
//json response
//        Context.Response.Write(js.Serialize(departmentBL.getDepartmentsByFaculty(faculty, university, branch)));

        return js.Serialize(departmentBL.getDepartmentsByFaculty(faculty, university, branch));

    } 
    catch (Exception ex)
    {

    }
    finally
    {
        departmentBL = null;
    }

//xml response
//return departmentList;

}
</department></department>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900