StatusCode 400 Bad Request in Entity Framework
I have this function for returning a list of decks in my DeckController that sends a request to my GetDecks function in my DeckDataController that works as expected. However when I try adding other methods I get a status code 400 bad request.
//DeckController.cs
// GET: Deck/List
/// <summary>
/// Get a list of all Decks
/// </summary>
/// <returns>returns a list of Decks</returns>
public ActionResult List()
{
// api string
string url = "DeckData/GetDecks";
//http request to the url
HttpResponseMessage response = client.GetAsync(url).Result;
Debug.WriteLine(response);
if (response.IsSuccessStatusCode)
{
IEnumerable<DeckDto> Decks = response.Content.ReadAsAsync<IEnumerable<DeckDto>>().Result;
return View(Decks);
}
else
{
return RedirectToAction("Error");
}
}
//DeckDataController.cs
/// Get a list of decks in the database alongside ok code (200)
/// </summary>
/// <returns>A list of decks</returns>
/// GET: api/DeckData/GetDecks
[HttpGet]
[ResponseType(typeof(IEnumerable<DeckDto>))]
public IHttpActionResult GetDecks()
{
// get the list of decks from the database
List<Deck> Decks = db.Decks.ToList();
// create an empty Deck data transfer object
List<DeckDto> DeckDtos = new List<DeckDto> { };
Debug.WriteLine("In GetDecks");
// for each deck create a new DeckDto and push it to the list of DeckDtos.
foreach (var deck in Decks)
{
DeckDto newDeck = new DeckDto
{
DeckID = deck.DeckID,
DeckTitle = deck.DeckTitle
};
DeckDtos.Add(newDeck);
}
return Ok(DeckDtos);
}
For example, adding the following function to my DeckDataController breaks my server.
// DeckDataController.cs
/// <summary>
/// Finds a deck based on the deckID
/// </summary>
/// <param name="id">DeckID</param>
/// <returns>returns a DeckDto object if found, otherwise NotFound object</returns>
[HttpGet]
[ResponseType(typeof(DeckDto))]
public IHttpActionResult FindDeck(int id)
{
// find the deck in the database
Deck deck = db.Decks.Find(id);
// if deck isnt found
if (deck == null)
{
return NotFound();
}
// create a data transfer object to send back
DeckDto DeckDto = new DeckDto
{
DeckID = deck.DeckID,
DeckTitle = deck.DeckTitle
};
return Ok(DeckDto);
}
but if I comment out the above function everything seems to be work fine.
from Recent Questions - Stack Overflow https://ift.tt/3sxqYAU
https://ift.tt/eA8V8J
Comments
Post a Comment