Alex Dn
Alex Dn

Reputation: 5553

ASP.NET WebService test form

Is it possible to configure default ASP.NET WebService test form to support JSON? I mean the test form that built in in .NET framework...

Currently I have a WebService that decorated with [ScriptService], but when I testing it using built in test form page, it returns XML...I assume, this happens because test page sends Content-Type XML by default.

Thanks

EDIT (Example):
I have class:

public class Person
{
  public string FirstName { get; set; }
  public string LastName { get; set; }
}

Now I have ASP.NET WebService:

[ScriptService]
public class PersonService : WebService
{
  [WebMethod]
  public Person GetDave()
  {
    Person dave = new Person();

    dave.FirstName = "Dave";
    dave.LastName = "Test";

    return dave;
  }
}

When I call this WebService from web page using jQuery AJAX, I receive JSON Person object {"FirstName":"Dave","LastName":"Test"} (not string) in JavaScript, but when I invoking this WebService from ASP.NET WebService Test Form (When I right click on ASMX file and use "Preview In Browser"),

enter image description here

It returns:

<?xml version="1.0" encoding="utf-8" ?> 
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <FirstName>Dave</FirstName> 
  <LastName>Test</LastName> 
</Person>

What want, is when I invoke the service from test page, to see the same output:

{"FirstName":"Dave","LastName":"Test"}

Upvotes: 1

Views: 877

Answers (3)

Alex Dn
Alex Dn

Reputation: 5553

It looks like I found a solution...it still not complete solution, but this is the way to go :)

In [Drive]:\[WindowsDir]\Microsoft.NET\Framework\[Version]\CONFIG folder, exists the file named DefaultWsdlHelpGenerator.aspx. This file contains the whole code needed to automatically generate test page using WSDL. Now I can use this code to write my own test page and make requests using jQuery and not using HTML form...then I can put in config file and this should work.

<webServices>
      <wsdlHelpGenerator href="WSTestPage.aspx"/>
</webServices>

Maybe somewhere exists more simple/ready way to do it, but I still not found it...

Upvotes: 1

Rajeev Bera
Rajeev Bera

Reputation: 2019

You can use the code below

[WebMethod(Description = "Some Description")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string FunctionName()
{           
    // Return JSON data
    JavaScriptSerializer js = new JavaScriptSerializer();
    string retJSON = js.Serialize(Object);
    return retJSON;
}

And also you need to add the reference.

Update

Here is the link which will explain about extending an existing ASP.NET Web Service to support JSON

Hope that helps

Upvotes: 1

Bibek Gautam
Bibek Gautam

Reputation: 592

Add following references first

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

use the below code in your method, for converting any data into JSON Data format in end

JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(dr);

dr is array of DataRows from DataTable.Hope this will help You.

Upvotes: 0

Related Questions