I have one application where some dummy data are kept :
public class ValuesController : ApiController
{
// GET api/values
public string Get()
{
List<Cars> carslist = new List<Cars>();
carslist.Add(new Cars { Id = "1", Color = "RED", CompanyName = "AUDI", Years = "1993", Model = "A4" });
carslist.Add(new Cars { Id = "2", Color = "BLUE", CompanyName = "MERCEDES", Years = "1992", Model = "BENZ" });
carslist.Add(new Cars { Id = "3", Color = "GREEN", CompanyName = "BMW", Years = "1990", Model = "C-CLASS" });
carslist.Add(new Cars { Id = "4", Color = "YELLOW", CompanyName = "VOLKSWAGEN", Years = "1989", Model = "JETTA" });
//return carslist;
//JavaScriptSerializer jss = new JavaScriptSerializer();
//string jsonstring = jss.Serialize(carslist);
//return jsonstring;
string JsonString = JsonConvert.SerializeObject(carslist);
return JsonString;
}
After JSON Formatting is done, i am requesting this particular data from another application, and Deserializing there, but it always showing error. The code from which i am accepting this data is below:
public ActionResult Index()
{
Uri url = new Uri(@"http://localhost:51933/api/Values");
WebRequest webRequest = WebRequest.Create(url);
WebResponse response = webRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
string responseData = streamReader.ReadToEnd().Trim();
var sdf = JsonConvert.DeserializeObject<List<Cars>>(responseData);
return View();
}
The Model is like this
public class Cars
{
public string Id { get; set; }
public string CompanyName { get; set; }
public string Model { get; set; }
public string Years { get; set; }
public string Color { get; set; }
}
public class ListOfCars
{
public List<Cars> listofcars { get; set; }
}
I am really not understanding, why the deserialization is not happening. Please help me out.
Thanks in advance.
Both the applications are running on localhost
This is the error :