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
<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?