How to return JSON from ASP.NET .asmx?

34,655

Solution 1

Do you have .NET 3.5 or greater installed?

ScriptServiceAttribute is in .NET 3.5 and 4.0.

Also, clear your ASP.NET temp files, the dynamic proxy could be cached.

Solution 2

I'm a bit late to this question, but hopefully this helps you or anyone else stumbling onto this thread later.

You definitely can use ASMX services to communicate in JSON.

Your service code looks okay. Since you aren't showing how you're calling it, I'll bet that's where your problem lies. One requirement for getting JSON out of ASMX "ScriptServices" is that you must call them with the correct content-type header and you must use a POST request. Scott Guthrie has a good post about the reasoning behind those requirements.

So, if you just request DemoService.asmx/GetCustomer in a browser, you're going to get XML. However, if you make a POST request with an application/json content-type, you'll get the same result serialized as JSON.

If you're using jQuery, here's an example of how you would request your DemoService on the client-side:

$.ajax({
  type: "POST",
  contentType: "application/json",
  url: "DemoService.asmx/GetCustomer",
  data: "{}",
  dataType: "json",
  success: function(response) {
    // Prints your "Microsoft" response to the browser console.
    console.log(response.d);
  }
});

More here: http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

Solution 3

Why not just use an ashx file? It's a generic handler. Very easy to use and return data. I use these often in place of creating a web service as they are much lighter.

An example of the implementation in the ashx would be:

// ASHX details
DataLayer dl = GetDataLayer();
List<SomeObject> lst = dl.ListSomeObjects();
string result = "";
if (lst != null)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    result = serializer.Serialize(lst);
}
context.Response.ContentType = "application/json";
context.Response.Write(result);
context.Response.End();

If you do need to use a web service though you could set the ResponseFormat. Check out this SO question that has what you are looking for:

How to let an ASMX file output JSON

Solution 4

This is what I do, though there is probably a better approach, it works for me:

[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string retrieveWorkActivities(int TrackNumber)
{
            String s = {'result': 'success'};
            return s.ToJSON();
}
Share:
34,655
ChrisP
Author by

ChrisP

Updated on February 02, 2020

Comments

  • ChrisP
    ChrisP over 4 years

    I have the following test method:

    Imports System.Web
    
    Imports System.Web.Services
    
    Imports System.Web.Services.Protocols
    
    
    <WebService(Namespace:="http://tempuri.org/")> _
    
    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    
    <System.Web.Script.Services.ScriptService()> _
    
    Public Class DemoService
     Inherits System.Web.Services.WebService
    
    <WebMethod()> _
    Public Function GetCustomer() As String
        Return "Microsoft"
    End Function
    
    
    End Class
    

    Even with the ResponseFormat attribute added the response is still being returned as XML rather than JSON.

    Thoughts appreciated.

  • MarkJ
    MarkJ over 14 years
    Don't you mean String s = "{'result': 'success'}"; ? And obviously you've posted C# and the question was VB.Net (I know everyone does that)
  • James Black
    James Black over 14 years
    @markJ - I didn't have an example in VB.NET, but I was mainly showing the annotations, but I didn't think about the language either. And I removed the double quote. Thanx.
  • ChrisP
    ChrisP over 14 years
    Target is .NET 3.5. Cleared temp files and still get same response.
  • rick schott
    rick schott over 14 years
    Your code looks solid, try changing your ScriptService tag to this, <ScriptService()> _.
  • Rohit Sharma
    Rohit Sharma over 13 years
    @chrisp Did you ever get your issue sorted out? If this answer or another helped you, you should set an accepted answer so that others reading this question can locate the solution that helped you.