c# - Wcf 返回通常的响应

标签 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  { 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 得到的响应,因此我无法将响应反序列化 到列表中。

<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>

契约(Contract)

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData/{value}") ]
List<tblcourse> GetData(string value);

反序列化时出错

Additional information: Unexpected character encountered while parsing value: <. Path '', line 0, position 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>

最佳答案

我认为您的 webinvoke 应该如下所示。 (指定响应格式)

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetData/{value}",ResponseFormat = WebMessageFormat.Json) ]
List<tblcourse> GetData(string value);

关于c# - Wcf 返回通常的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27372225/

相关文章:

c# - 为字母绘制矩形框

c# - WPF 淡出动画 - 不能再改变不透明度

c# - ASP.NET MVC 将 ViewResult 作为 html 文件返回

asp.net-mvc - 下拉列表更改的 MVC 4 回发

c# - 将数组的 JSON 数组反序列化为 c# 类

c# - 检查是否至少有一个对象不为零

javascript - 在 MVC 项目中,当下拉列表更改值时如何更新模型?

wcf - 如何将位置 header 设置为 WCF 4.0 REST 中另一个服务的 UriTemplate 没有魔术字符串?

c# - 如何在IIS上产生负载?