StackOverflow.
The issue I am having today is when I send a get request to localhost:xxxxx trying to obtain a singular food item by ID, I receive a 500 internal server error.
What I do not understand about this is, I went through the code in debugging and it shows that it finds the item from my database with all the information it needs, but when it is sending it in a "OK(food)", I receive a 500 internal service error even though it has the information.
I am pretty new to web api, so some explanation would be appreciated. I am using Visual Studio 2015, Entity Frame Work 6, and a SQL Management Server DB.
I am sorry for posting links, it says I need 10 reputation to post images
protected void Page_Load(object sender, EventArgs e)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:63591/");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/foods/" + Request.QueryString["ID"]).Result;
if (response.IsSuccessStatusCode)
{
string foodstring = response.Content.ReadAsStringAsync().Result;
Food editFood = JsonConvert.DeserializeObject<Food>(foodstring);
EditFoodName.Text = editFood.FoodName;
EditCalories.Text = editFood.Calories.ToString();
EditNotes.Text = editFood.Notes;
}
}
[ResponseType(typeof(Food))]
public IHttpActionResult GetFood(int id)
{
Food food = db.Foods.Find(id);
if (food == null)
{
return NotFound();
}
return Ok(food);
}
public partial class Food
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Food()
{
this.MealFoods = new HashSet<MealFood>();
}
public int FoodID { get; set; }
public string FoodName { get; set; }
public int Calories { get; set; }
public string Notes { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<MealFood> MealFoods { get; set; }
}