开发者

How to translate PHP JSON web service to C#?

开发者 https://www.devze.com 2023-03-09 14:16 出处:网络
I\'ve been writing an iPhone app that could send JSON to a PHP file and been fairly successful doing this.However, now that I want to move on to ASP C# and create an equivalent .NET web service akin t

I've been writing an iPhone app that could send JSON to a PHP file and been fairly successful doing this. However, now that I want to move on to ASP C# and create an equivalent .NET web service akin to the PHP I wrote earlier to decode and encode the JSON, I'm completely clueless.

In PHP I do this:

$handle = fopen('php://input','r');
$jsonInput开发者_如何学Go = fgets($handle);
$decoded = json_decode($jsonInput,true);

to receive the JSON POST data and decode it . Then using this:

$data = $decoded;
header('Content-Type: application/json');
echo json_encode($data);

How do I write an equivalent C# web service? Thanks.


It very simple to use WCF REST. follow steps as this link below:

http://blogs.msdn.com/b/kaevans/archive/2008/04/03/creating-restful-services-using-wcf.aspx

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebGet(UriTemplate="customers/{id}", ResponseFormat=WebMessageFormat.Json)]
    Customer GetCustomer(string id);

    [OperationContract]
    [WebInvoke(UriTemplate="customers", ResponseFormat=WebMessageFormat.Json)]
    Customer PostCustomer(Customer c);
}

so result will get in Json format encode by WCF.

0

精彩评论

暂无评论...
验证码 换一张
取 消