【发布时间】: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;
}
}
【问题讨论】:
-
显而易见的答案是逃避它......但是,您能否展示一些您尝试序列化的数据,以便我们可以看到您的确切问题?
-
是的,我现在已经更新了。但是我尝试从我的函数中删除所有代码,仍然出现此错误..