Serving JSON from ASP.NET

Last post 10-13-2007 7:45 PM by KaziManzurRashid. 23 replies.

Sort Posts:

  • Re: Serving JSON from ASP.NET

    09-26-2007, 2:40 PM
    • Member
      105 point Member
    • dotnetCarpenter
    • Member since 08-08-2006, 8:30 PM
    • Copenhagen, Denmark
    • Posts 48

    [ScriptService]
    [WebService(Namespace = "http://mapserv.utah.gov/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class UTSGID_GeoLocator : System.Web.Services.WebService

     is above my class

    I'm sorry but I thought you used the above attributes...

    I think your problem is that you don't have the () after the ScriptService attribute. Your webservice should look something like this:

     <%@ WebService Language="C#" Class="UTSGID_GeoLocator" %>

    using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Web.Script.Services;
    using System.Web.Script.Serialization;

    [WebService(Namespace = "http://mapserv.utah.gov/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.None)]
    [ScriptService()]
    public class UTSGID_GeoLocator : System.Web.Services.WebService

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetHelloWorld() {
            return "Hello World";
        }
        
    }

    If you haven't already, I strongly suggest you install Firebug and look at the error HTML the webservice throws at you.

    Cheers, Jon. 

    Filed under:
  • Re: Serving JSON from ASP.NET

    09-26-2007, 3:12 PM
    • Member
      16 point Member
    • stevegourley
    • Member since 09-25-2007, 7:04 PM
    • Posts 17

     my bad I guess.  I wasn't using an ashx page i'm using an asmx page with a .cs file.  The code above was in the .cs file.

  • Re: Serving JSON from ASP.NET

    09-26-2007, 3:51 PM
    • Member
      105 point Member
    • dotnetCarpenter
    • Member since 08-08-2006, 8:30 PM
    • Copenhagen, Denmark
    • Posts 48

    no no no. You should use asmx pages.. I'll create a demo project for you...
     

  • Re: Serving JSON from ASP.NET

    09-27-2007, 2:17 PM
    • Member
      4 point Member
    • tcboring@work
    • Member since 09-27-2007, 5:59 PM
    • Posts 7

    Check out the Professional AJAX project at Codeplex.com

     http://www.codeplex.com/AjaxPro

    I use it here at work INSTEAD OF the Microsoft ASP.NET AJAX Extensions, and it's much lighter, faster, and easier to deal with.

  • Re: Serving JSON from ASP.NET

    10-06-2007, 12:29 PM
    • Member
      76 point Member
    • hazlema
    • Member since 11-12-2006, 12:45 AM
    • Manchester, NH
    • Posts 24

    I couldn't get it to work as well, so I did this really ugly hack.  It probably breaks somthing down the road but it works.

    1        [WebMethod(false)]
    2        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    3        public string GetSamples(string Sample)
    4        {
    5            Portfolio Portfolio = new Portfolio();
    6            Context.Response.Write(Portfolio.SampleJSON(Sample));
    7            Context.Response.End();
    8    
    9            return "";
    10       }
    
      
    Matthew Hazlett
    Clarity Computers
    http://www.devclarity.com
  • Re: Serving JSON from ASP.NET

    10-08-2007, 8:02 AM

    As far as I know its not currently possible to return JSON in asp.net,  it could be an inadvertant policy issue where microsoft doesnt want people to use other products just like datatable which is not returned as webservice. But you can always convert xml to json.

     

    However webservice can return JSON data is when its called from within ASP.NET AJAX pages using the ScriptManager - which may not be sufficent if services are ultimately going to be called from a php application etc...

     You can however implement your own IHttpHandlerFactor and IHttpHandler classes.  They can then intercept the webservice calls and check classes, which return JSON using the JavaScriptSerializer, or the generic WebServiceHandlerFactory().

    the flag works out to be "application/OrYxJson" within the Context-Type property of the request.  If this property is set, JSON gets returned, otherwise you get good old SOAP.

    ..

    Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
  • Re: Serving JSON from ASP.NET

    10-08-2007, 9:33 AM
    • Member
      6 point Member
    • p5tech
    • Member since 05-18-2007, 2:12 PM
    • Posts 5

     You said:

    "As far as I know its not currently possible to return JSON in asp.net,  it could be an inadvertant policy issue where microsoft doesnt want people to use other products just like datatable which is not returned as webservice. But you can always convert xml to json."

    I'm not sure what that means, but of course you can return JSON.  A webservice can return it natively with or without ScriptManager.  A page can return it by outputting nothing but a JSON string. 

     

    -Greg
     

  • Re: Serving JSON from ASP.NET

    10-08-2007, 9:41 AM

    Hi Greg,

    I certainly agree that a webservice can return it natively with or without Script Manager, however not that of Asp.net as I know of which is why I said it could be a policy issue. Because one of the tenets of webservice it should provide a service in any form.  

     

    However I will be so glad to learn if there is a way to return JSON without my above suggestions of script manager, or other ones. I am here to learn.

     

    Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
  • Re: Serving JSON from ASP.NET

    10-13-2007, 7:45 PM
    • Contributor
      4,792 point Contributor
    • KaziManzurRashid
    • Member since 03-09-2003, 11:04 AM
    • Dhaka, Bangladesh
    • Posts 882
    Long Live .NET
    Kazi Manzur Rashid (Amit)
    _________________________
    Web: http //dotnetshoutout.com
    Blog: http://weblogs.asp.net/rashid
    Twitter: http://twitter.com/manzurrashid
Page 2 of 2 (24 items) < Previous 1 2