【问题标题】:How to post json data to wcf service如何将 json 数据发布到 wcf 服务
【发布时间】:2017-12-11 08:59:41
【问题描述】:

如何通过传递 json 来发布调用 SaveCorporateProfile 函数

[DataContract]
public class myclass
{
    public string CompanyCode { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}



 [ServiceContract]
    public interface ICustProfileService
    {
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/SaveCorporateProfile?dt={dt}")]
        string SaveCorporateProfile(myclass dt);
    }

public string SaveCorporateProfile(myclass dt)
        {          

            return "success";
        }

【问题讨论】:

标签: json wcf


【解决方案1】:

你可以试试这个;

首先,你应该像这样编辑你的myclass

[DataContract]
public class myclass
{

    [DataMember]
    public string CompanyCode { get; set; }

    [DataMember]
    public string Username { get; set; }

    [DataMember]
    public string Password { get; set; }
}

然后你的界面是这样的;

 [ServiceContract]
public interface ICustProfileService
{
    [OperationContract]
    string SaveCorporateProfile(myclass dt);
}

然后你实现你的 .svc 文件来使用这个接口

 public class CustProfileService: ICustProfileService
{
  [WebInvoke(Method = "POST",UriTemplate = "/SaveCorporateProfile?dt={dt}"
   BodyStyle = WebMessageBodyStyle.Bare,
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json)]

    public string SaveCorporateProfile(myclass dt)
    {          

        return "success";
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2015-02-14
    相关资源
    最近更新 更多