Receiving 500 internal server error even though I receive data from LINQ

asp.net-web-api c# entity-framework-6 https

Question

StackOverflow.

The problem I'm facing right now is that whenever I attempt to fetch a single food item by ID using a get request to localhost:xxxxx, I get a 500 internal server error.

I looked through the code in debugging and it showed that it retrieves the item from my database with all the information it requires, but when it is delivering it in a "OK(food)," I got a 500 internal service error even though it has the information. This is what I do not understand about it.

Since I am relatively new to web api, I would appreciate an explanation. Entity Framework 6 is what I'm using, along with a SQL Management Server DB, and Visual Studio 2015.

It says I need 10 reputation to submit photos, therefore I'm sorry for sharing links.

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; }
}

https://i.imgur.com/8h6EaZM.png

https://i.imgur.com/YkD8Ijp.png

1
1
2/5/2018 7:37:23 AM

Accepted Answer

ZZZ_tmp
0
2/6/2018 11:34:54 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow