ASP.从Web服务方法返回对象列表
本文关键字:返回 对象 列表 方法 服务 Web ASP | 更新日期: 2023-09-27 18:02:40
我想知道如何返回对象列表而不是字符串列表?
返回字符串列表工作,但当我用对象替换所有内容时,它失败了。
这是我的Web Service
[WebMethod]
public List<OpProduct> SearchProduct(string name) {
BLProduct blProduct = new BLProduct();
List<OpProduct> result = byproduct.SearchProducts(name);
return result;
}
这是我的业务逻辑层
public List<OpProduct> SearchProducts(string name)
{
List<OpProduct> result = (from item in db.OpProducts where item.Name.StartsWith(name) select item).ToList<OpProduct>();
return result;
}
这是我的错误信息(对不起,无法翻译这个)
系统。InvalidOperationException:这是一个错误生成xml文档。——>系统。InvalidOperationException:她甚至还经常在一辆小货车上行驶序列化对象类型为OpProduct。bijSystem.Xml.Serialization.XmlSerializationWriter.WriteStartElement(字符串名称,字符串n,对象0,布尔写前缀,XmlSerializerNamespaces xmlns) bijMicrosoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write9_OpProduct(字符串n,字符串n, OpProduct 0, Boolean isNullable, Boolean needType)
bijMicrosoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5_OpGeheugen(字符串n,字符串n, OpGeheugen 0, Boolean isNullable, Boolean needType)
bijMicrosoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write9_OpProduct(字符串n,字符串n, OpProduct 0, Boolean isNullable, Boolean needType)
bijMicrosoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write10_ArrayOfOpProduct(对象o) bijMicrosoft.Xml.Serialization.GeneratedAssembly.ListOfOpProductSerializer.Serialize(对象objectToSerialize, XmlSerializationWriter)System.Xml.Serialization.XmlSerializer。序列化(XmlWriter XmlWriter,XmlSerializerNamespaces名称空间,String encodingStyle字符串id)—Einde van intern uitonderingstackpad—bijSystem.Xml.Serialization.XmlSerializer。序列化(XmlWriter XmlWriter,XmlSerializerNamespaces名称空间,String encodingStyle字符串id) bijSystem.Xml.Serialization.XmlSerializer.Serialize (TextWritertextWriter, Object 0, XmlSerializerNamespaces(命名空间)bijSystem.Web.Services.Protocols.XmlReturnWriter.Write (HttpResponse输出流,对象返回值)bijSystem.Web.Services.Protocols.HttpServerProtocol.WriteReturns(对象[]返回值,输出流)bijSystem.Web.Services.Protocols.WebServiceHandler.WriteReturns(对象[]returnvalue) bijSystem.Web.Services.Protocols.WebServiceHandler.Invoke ()
有人能帮我吗?
默认情况下,只有系统基本类型和这些基本类型的数组(如int, float, double, string)可以用作返回值。
如果您想使用自己的类作为参数,则必须使用XML定义将它们定义为复杂类型。这里有一个MSDN链接https://msdn.microsoft.com/en-us/library/aa480498.aspx