webapi用的是http协议,webservice用的是soap协议
webapi无状态,相对webservice更轻量级。webapi支持如get,post等http操作 轻量级
mvc上面的就是webapi
// GET api/values
/// <summary>
/// 客户端调用某个Action方法并希望以JSON的格式返回请求的数据,使用JsonResult
/// </summary>
/// <returns></returns>
public /*string*/JsonResult<List<User>> GetAll()
{
List<User> userInfoList = new List<User>();
SqlConnection conn = new SqlConnection(constr);
string text = "userinfo_get";
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(text, conn);
DataSet ds = new DataSet("Students");
da.Fill(ds);
DataTable dt = ds.Tables[0];
foreach (DataRow row in dt.Rows)
{
User user = new Models.User();
user.Id = Convert.ToInt16(row["Id"]);
user.Name = Convert.ToString(row["Name"]);
user.Sex = Convert.ToString(row["Sex"]);
user.Age = Convert.ToInt16(row["Age"]);
userInfoList.Add(user);
}
conn.Close();
return Json<List<User>>(userInfoList);
///xml不可行
//XmlSerializer serializer = new XmlSerializer(typeof(List<User>));
//return serializer.Serialize(userInfoList);
//return JsonConvert.SerializeObject(userInfoList);
}