将对象传递到具有Dictionary的WebMethod<字符串,int>参数

pass object to WebMethod with Dictionary<string, int> paramter

本文关键字:字符串 lt 参数 gt int WebMethod 对象 Dictionary      更新时间:2023-09-26

我有这个错误无法转换''u0027System类型的对象。字符串''u0027以键入''u0027System。集合。通用的字典2[System.String,System.Int32]'u0027","StackTrace":" 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.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)'r'n at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary 2 rawParams)。网状物剧本服务。WebServiceMethodData。系统上的CallMethodFromRawParams(对象目标,IDictionary 2 parameters)'r'n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary 2 rawParams)''r''n。网状物剧本服务。RestHandler。ExecuteWebServiceCall(HttpContext上下文,WebServiceMethodData methodData)","ExceptionType":"系统。无效操作异常

当我用字典将数组传递给WebMethod时

如果它是webapi的webmethod,那么你不能将列表或字典类型的对象传递给方法,你只需要传递字符串参数,要传递这个对象,你需要将它们序列化为字符串,然后你可以使用该字符串将它们反序列化回你的列表或字典对象。

Webmethod消费代码:

Dictionary<string,int> objstring = new Dictionary<string,int>
var serializer = new JavaScriptSerializer();
var serializedstring = serializer.Serialize(objstring);

然后传递serializedstring-in参数。

在反序列化时执行同样的操作,只需从webapi方法中的序列化程序对象调用反序列化方法。

我希望这对你有帮助。