【问题标题】:StatusCode 400 Bad Request in Entity Framework实体框架中的 StatusCode 400 错误请求
【发布时间】:2021-02-26 22:47:19
【问题描述】:

我有这个函数用于返回我的 DeckController 中的卡组列表,该函数向我的 DeckDataController 中的 GetDecks 函数发送请求,该函数按预期工作。但是,当我尝试添加其他方法时,我收到状态代码 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);
    }

例如,将以下函数添加到我的 DeckDataController 会破坏我的服务器。

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

但如果我注释掉上述函数,一切似乎都可以正常工作。

【问题讨论】:

    标签: c# asp.net entity-framework


    【解决方案1】:

    为什么不使用此代码:

    
    var deck = db.Decks.Where(i=>i.DeskID==id).FirstOrDefault();
    ....
    
    

    【讨论】:

      猜你喜欢
      • 2017-04-07
      • 2023-03-21
      • 1970-01-01
      • 2013-04-05
      • 2015-08-12
      • 2020-06-10
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      相关资源
      最近更新 更多