Can't get HTTP PUT request to work in ASP.NET Core

前端 未结 3 803
萌比男神i
萌比男神i 2021-01-25 08:22

I\'m trying to update an entry in the game table. However, my PUT request in ASP.NET never seems to trigger, and I can\'t figure out why.

This is controller

相关标签:
3条回答
  • 2021-01-25 09:04

    The route template parameter {update.GameID} is not standard to what is suggested by documentation

    Assuming the game id is an integer review the following

    //PUT .../game/5
    [Route("game/{id:int}")]
    [HttpPut]
    public IActionResult updateGame(int id, [FromBody]Game update) {
        //...
    }
    

    Reference Routing to controller actions in ASP.NET Core

    I would also suggest you review the logic of the action as I do not believe it is doing what you think it does with updating the entity returned from the context.

    0 讨论(0)
  • 2021-01-25 09:15

    If you are using PUT request you need to add a resource id either to update or create new - so just don't combine your id with your object

    [HttpPut("game/{id}")]
    public IActionResult UpdateGame(int id, [FromBody]Game update) {
        //...
    }
    

    If you are using Asp.net Core you can just re-write your URL on your HTTP verbs attribute like the code above - So pass your resource id in the URL and bind your object in the body - Your URL should read as https://localhost:44359/api/v1/game/2

    Hope this helps you - Happy coding !!

    0 讨论(0)
  • 2021-01-25 09:23

    Can you modify your defining route just like

    [Route("game")]
    [HttpPut]
    public IActionResult updateGame([FromBody]Game update)
    {
       //your code
    }
    

    And call from angular like

    putGame(game:Game){
        return this._http.put(this.url + "game", game);
    }
    

    you can receive gameid from game object so don't need from url

    0 讨论(0)
提交回复
热议问题