Here is my Api implementation
Models
public class Cars
{
public string carName;
public string carRating;
public string carYear;
}
my api controller
public class DefaultController : ApiController
{
public HttpResponseMessage GetCarses()
{
List<Cars> carList = new List<Cars>();
carList.Add(new Cars
{
carName = "a",
carRating = "b",
carYear = "c"
});
carList.Add(new Cars
{
carName = "d",
carRating = "e",
carYear = "f"
});
return Request.CreateResponse(HttpStatusCode.OK, carList); ;
}
}
and my response from HttpClient
String jsonData;
string url =
String.Format(
@" http://localhost:37266/api/Default/GetCars");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
jsonData = reader.ReadToEnd();
}
//Console.WriteLine(jsonData);
}
var cars = new JavaScriptSerializer().Deserialize<List<Cars>>(jsonData);
var ss = cars;
Dont forget to add
protected void Application_Start()
{
//add this line if not
GlobalConfiguration.Configure(WebApiConfig.Register);
...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…