gpt4 book ai didi

asp.net - 将多个参数传递给 Web 服务时出现问题

转载 作者:行者123 更新时间:2023-12-01 01:19:58 24 4
gpt4 key购买 nike

我有一个简单的 Web 服务方法定义为:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string MyWebMethod(string foo, string bar)
{
// DataContractJsonSerializer to deserialize foo and bar to
// their respective FooClass and BarClass objects.

return "{\"Message\":\"Everything is a-ok!\"}";
}

我将通过以下方式从客户端调用它:

var myParams = { "foo":{"name":"Bob Smith", "age":50},"bar":{"color":"blue","size":"large","quantity":2} };

$.ajax({
type: 'POST',
url: 'https://mydomain.com/WebServices/TestSvc.asmx/MyWebMethod',
data: JSON.stringify(myParams),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (response, status) {
alert('Yay!');
},
error: function (xhr, err) {
alert('Boo-urns!');
}
});

但是,这会产生以下错误(永远不会命中 MyWebMethod() 中第一行的断点):

{"Message":"No parameterless constructor defined for type of \u0027System.String\u0027.","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary2
dictionary, Type type,
JavaScriptSerializer serializer,
Boolean throwOnError, Object&
convertedObject)\r\n at
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object
o, Type type, JavaScriptSerializer
serializer, Boolean throwOnError,
Object& convertedObject)\r\n at
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object
o, Type type, JavaScriptSerializer
serializer, Boolean throwOnError,
Object& convertedObject)\r\n at
System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary
2 rawParams)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.MissingMethodException"}

我想传入两个字符串参数并使用 DataContractJsonSerializer 编写新的 Foo 和 Bar 对象。我错过了什么吗?

最佳答案

对于服务中的代码,您需要使用对象而不是字符串来表示“foo”和“bar”。然后使用Newtonsoft.Json的函数解析将该对象转换为Json对象,然后构建强类型对象。

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string MyWebMethod(object foo, object bar)
{
// DataContractJsonSerializer to deserialize foo and bar to
// their respective FooClass and BarClass objects.

//parse object to JObject using NewtonJson
JObject jsonFoo = JObject.Parse(JsonConvert.SerializeObject(foo));
JObject jsonBar = JObject.Parse(JsonConvert.SerializeObject(bar));
Foo fo = new Foo(jsonFoo);
Bar ba = new Bar(jsonBar);

return "{\"Message\":\"Everything is a-ok!\"}";
}
public class Foo{
public Foo(JObject jsonFoo){
if (json["prop1"] != null) prop1= json["prop1"].Value<long>();
if (json["prop2"] != null) prop2= (string)json["prop2"];
if (json["prop3"] != null) prop3= (string)json["prop3"];
}
}

关于asp.net - 将多个参数传递给 Web 服务时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3872982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com