C# Wcf返回通常的响应
我正试图返回一个列表C# Wcf返回通常的响应,c#,asp.net-mvc-4,wcf-rest,C#,Asp.net Mvc 4,Wcf Rest,我正试图返回一个列表 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 {
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;
}
合同
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData/{value}") ]
List<tblcourse> GetData(string value);
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>
我认为您的webinvoke应该是这样的。(指定响应格式)
[运营合同]
[WebInvoke(Method=“GET”,UriTemplate=“GetData/{value}”,ResponseFormat=WebMessageFormat.Json)]
列表GetData(字符串值);
请提供您的服务合同的外观。@dotnetstep:请查看我的编辑。我无法反序列化这样的字符串。@marc_s:我得到的错误是附加信息:解析值时遇到意外字符:您是否在配置文件中配置了WebHttpBiding和webhttp行为?JsonConvert.DeserializeObject(strResponse1)代码>您使用的是(来自MSDN)的WebHttpBinding
,WCF Web编程模型允许开发人员通过使用“普通的旧XML”(POX)样式的消息传递而不是基于SOAP的消息传递的HTTP请求公开WCF Web服务。JSON序列化程序将如何对XML进行反序列化?+一个用于理解XML和JSON的细微差别以及XML如何不是JSON的序列化程序。@dotnetstep:工作起来很有魅力。谢谢。@dotnetstep:我可以减少代码,仍然得到相同的结果吗。?我可以跳过foreach循环吗?如果两个实体相同,那么应该得到相同的结果。您可以尝试使用return only var u=(从t.tblcourses中的g选择new{g.C_Id,g.C_Name});返回u;
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData/{value}") ]
List<tblcourse> GetData(string value);
[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; }
}
<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>
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData/{value}",ResponseFormat = WebMessageFormat.Json) ]
List<tblcourse> GetData(string value);