I am going nuts here. I\'ve looked at the following entries and none of them are correcting the aberrant behavior I am seeing:
What's the "Content-Type" set to on the request to the method?
From what I've done with the ASP.NET, if it's set to text/xml
, you'll get back XML; but if it's set to application/json
, you'll get JSON back.
Why don't you migrate your ASMX webservice to WCF?
The WCF API in .NET Framework 3.5 supports JSON web services natively.
In addition Microsoft declared ASMX as "legacy technology", and suggests "Web services and XML Web service clients should now be created using Windows Communication Foundation (WCF)". (Source).
You may want to check out these links to get started:
In addition, you may also want to read through the following example which I "extracted" from a self-hosted WCF project of mine. Self-hosted WCF services do not require IIS, but can be served from any managed .NET application. This example is being hosted in a very simple C# Console Application:
IContract.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace MyFirstWCF
{
[ServiceContract]
public interface IContract
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/CustomerName/{CustomerID}")]
string GET_CustomerName(string CustomerID);
}
}
Service.cs
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
namespace MyFirstWCF
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.NotAllowed)]
public class Service : IContract
{
public string GET_CustomerName(string CustomerID)
{
return "Customer Name: " + CustomerID;
}
}
}
WCFHost.cs (Console Application)
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
using System.Threading;
using System.Text;
namespace MyFirstWCF
{
class Program
{
private static WebServiceHost M_HostWeb = null;
static void Main(string[] args)
{
M_HostWeb = new WebServiceHost(typeof(MyFirstWCF.Service));
M_HostWeb.Open();
Console.WriteLine("HOST OPEN");
Console.ReadKey();
M_HostWeb.Close();
}
}
}
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="MyFirstWCF.Service">
<endpoint address="http://127.0.0.1:8000/api"
binding="webHttpBinding"
contract="MyFirstWCF.IContract" />
</service>
</services>
</system.serviceModel>
</configuration>
The above example is very basic. If you build a request with Fiddler to http://127.0.0.1:8000/api/CustomerName/1000
it will simply return "Customer Name: 1000"
.
Make sure to set the content-type: application/json
in the request header. To return more complex data structures, you will have to use Data Contracts. These are constructed as follows:
[DataContract]
public class POSITION
{
[DataMember]
public int AssetID { get; set; }
[DataMember]
public decimal Latitude { get; set; }
[DataMember]
public decimal Longitude { get; set; }
}
You need to add .NET References to System.RuntimeSerialization
, System.ServiceModel
and System.ServiceModel.Web
for this example project to compile.