我正在尝试返回一个列表
public List<tblcourse> GetData(string value)
{
testEntities1 db = new testEntities1();
int v = Convert.ToInt32(value);
testEntities1 t = new testEntities1();
var u = (from g in t.tblcourses
select new { g.C_Id, g.C_Name }).ToList();
List<tblcourse> lisstt = new List<tblcourse>();
foreach (var item in u)
{
tblcourse b = new tblcourse();
b.C_Id = item.C_Id;
b.C_Name = item.C_Name;
lisstt.Add(b);
}
return lisstt;
}
当我使用此服务时,从客户端,当我执行客户端代码时,我从服务获得以下响应
string strServiceUrl1 = "htttp://localhost:1967/Service1.svc/GetData/" + id;
HttpWebRequest objHttpWebRequest1 = WebRequest.Create(strServiceUrl1) as HttpWebRequest;
objHttpWebRequest1.Method = "GET";
objHttpWebRequest1.ContentType = "application/json-urlencoded";
StreamReader onjStreamReader1 = new StreamReader(objHttpWebRequest1.GetResponse().GetResponseStream());
string strResponse1 = onjStreamReader1.ReadToEnd().ToString();
List<Course> cc = JsonConvert.DeserializeObject<List<Course>>(strResponse1);
return View(cc);
以下是我作为response
得到的回复,因此我无法deserialize
将回复列入列表。
<ArrayOftblcourse xmlns="htttp://schemas.datacontract.org/2004/07/app2"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<tblcourse>
<C_Id>1</C_Id>
<C_Name>DOt Net</C_Name>
</tblcourse>
<tblcourse>
<C_Id>2</C_Id>
<C_Name>EF</C_Name>
</tblcourse>
<tblcourse>
<C_Id>3</C_Id>
<C_Name>MVC</C_Name>
</tblcourse>
<tblcourse>
<C_Id>4</C_Id>
<C_Name>MS SQL</C_Name>
</tblcourse>
</ArrayOftblcourse>
合同
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData/{value}") ]
List<tblcourse> GetData(string value);
反序列化时出错
其他信息:解析值时遇到意外的字符:&lt;。路径'',第0行,第0位。
自动生成的tbclcourses
[DataContract]
[KnownType(typeof(tblstudent))]
public partial class tblcourse
{
public tblcourse()
{
this.tblstudents = new HashSet<tblstudent>();
}
[DataMember]
public int C_Id { get; set; }
[DataMember]
public string C_Name { get; set; }
public virtual ICollection<tblstudent> tblstudents { get; set; }
}
web.config
个文件webHttpBinding:
<webHttpBinding>
<binding name="webHttpBinding"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
transferMode="StreamedResponse">
<security mode="None">
<transport clientCredentialType="Basic"></transport>
</security>
<readerQuotas maxArrayLength="100000" maxStringContentLength="2147483647" />
</binding>
</webHttpBinding>
答案 0 :(得分:3)
我认为你的webinvoke应该是这样的。 (指定响应格式)
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData/{value}",ResponseFormat = WebMessageFormat.Json) ]
List<tblcourse> GetData(string value);