【问题标题】:Sending unescaped JSON data to WCF via POST通过 POST 将未转义的 JSON 数据发送到 WCF
【发布时间】:2014-06-09 11:08:20
【问题描述】:

我有一个完整的 WCF 服务,当我通过 POST 方法发送转义的 JSON 数据时,它工作正常。但是当我发送未转义的 JSON 时会出现错误错误请求。谁能告诉我一个解决方案。这是我在 WCF 中使用的接口代码。

  [ServiceContract]
  public interface IService1
  {
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "LoginCloudUser", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    DNNLoginResponse LoginCloudUser(string args);

    [OperationContract]
    UserCredential GetDataUsingDataContract(UserCredential composite);

    // TODO: Add your service operations here
  }

更新:我正在使用 JSON.Net 序列化 JSON。但是我尝试删除所有代码并将参数作为字符串返回。如果 JSON 没有转义,它仍然会出错。

public DNNLoginResponse LoginCloudUser(string args)
    {
        try
        {
            JsonSerializerSettings jss = new JsonSerializerSettings();
            jss.StringEscapeHandling = StringEscapeHandling.Default; 
            DNNLogin du = JsonConvert.DeserializeObject<DNNLogin>(args, jss);
            bool validateresult = DNNLogin.ValidateUser(du); 

            DNNLoginResponse dlogres = new DNNLoginResponse(); 
            dlogres.result  = validateresult;
            dlogres.resulttype = "Success"; 
            dlogres.userid = du.username;

            return dlogres; //JsonConvert.SerializeObject(dlogres, Formatting.None, jss); 
        }
        catch
        { 
            DNNLoginResponse dlogres = new DNNLoginResponse(); 
            dlogres.result = false;
            dlogres.resulttype = "Internal Error";
            dlogres.userid = string.Empty;

            return dlogres;  
        }
    }

【问题讨论】:

  • 显而易见的答案是逃避它......但是,您能否展示一些您尝试序列化的数据,以便我们可以看到您的确切问题?
  • 是的,我现在已经更新了。但是我尝试从我的函数中删除所有代码,仍然出现此错误..

标签: c# json wcf post


【解决方案1】:

我已经删除了 JSON.NEt 并将代码更改为这个,它工作得非常好。

 [ServiceContract]
 public interface IService1
 {
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "LoginCloudUser", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    DNNLoginResponse LoginCloudUser(DNNLogin args);

    [OperationContract]
    UserCredential GetDataUsingDataContract(UserCredential composite);

    // TODO: Add your service operations here
}

还有功能码。

       public DNNLoginResponse LoginCloudUser(DNNLogin args)
      {
        try
        {

            bool validateresult = DNNLogin.ValidateUser(args); 

            DNNLoginResponse dlogres = new DNNLoginResponse();
            if (validateresult)
            {
                dlogres.result = validateresult;
                dlogres.resulttype =  "Success" ;
                dlogres.userid = args.username;
            }
            else
            {
                dlogres.result = validateresult;
                dlogres.resulttype = "Login Error";
                dlogres.userid = string.Empty;

            }


            return dlogres; //JsonConvert.SerializeObject(dlogres, Formatting.None, jss); 
        }
        catch
        { 
            DNNLoginResponse dlogres = new DNNLoginResponse(); 
            dlogres.result = false;
            dlogres.resulttype = "Internal Error";
            dlogres.userid = string.Empty;

            return dlogres;  
        }
    }

【讨论】:

    猜你喜欢
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2013-02-05
    • 2014-06-10
    • 2019-04-12
    • 1970-01-01
    • 2023-02-06
    相关资源
    最近更新 更多