我已经创建了一个WCF RESTful Service方法,如下所示:Id为101和102
public EmployeeJSON GetEmployeeJSON(string id)
{
List<EmployeeJSON> employees = new List<EmployeeJSON>()
{
new EmployeeJSON() {Name="Sumanth",Id=101,Salary=5000.00 },
new EmployeeJSON() {Name="Ehsan",Id=102,Salary=6000.00 },
};
var Employee = (from x in employees
where x.Id.ToString() == id
select x);
return Employee as EmployeeJSON;
}
在任何时间点,对于传递的Id值,我将从客户端
获得1条记录声明如下
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetJson/{id}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
EmployeeJSON GetEmployeeJSON(string id);
客户端代码如下所示
protected void btn_Click(object sender, EventArgs e)
{
var request = (HttpWebRequest)WebRequest.Create("http://localhost:1249/Service1.svc/GetJson/101");
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
txtEmpId.Text = reader.ReadToEnd();
}
对于101和102以及任何参数值说100,它总是返回错误,如下所示
{"GetEmployeeJSONResult":null}
无法弄明白为什么......
答案 0 :(得分:0)
对于此函数public EmployeeJSON GetEmployeeJSON(string id)
,返回类型的更改如下所示
return Employee.FirstOrDefault() as EmployeeJSON;