Search code examples
c#asp.netjsonwebmethod

ASP.Net - Json webmethod result in brower


I am trying to call a json webmethod (present in the code behind file) from browser. But get no output!

In my json.aspx.cs page: the web method is

[System.Web.Script.Services.ScriptService]
        public class _default : System.Web.Services.WebService
        {
            [WebMethod]
            [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
            public string[] UserDetails()
            {
                return new string[] { "abc", "def" };
            }
        }

When I try the following url from browser:

http://www.mydomain/json.aspx/UserDetails

I get no result! (The page is blank) I expect - the browser to show - { "abc", "def" }

Correct me if I am doing something wrong. My intention is to get the result as plain text in browser. Is it not possible to achieve if I place the webmethod in the code behind? I don't want to create a separate service for this...


Solution

  • Actually none of the above worked.

    Now instead of web form-code behind, I created a new asmx page & put it in the code behind Now, it shown some config issues: To solve it, I added:

    <system.web>
        <webServices>
          <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
          </protocols>
        </webServices>
    

    ............

    in thw eb config file.

    Now I am getting the output in the browser.

    The only thing remaining now - is I want it as plain text (json).

    But I get it as xml:

    <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
    <string>abc</string>
    <string>def</string>
    </ArrayOfString>
    

    I am looking to fix this also.