5

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 in ASP.NET:

[Route("game/{update.GameID}")]
[HttpPut]
public IActionResult updateGame([FromBody]Game update)
{
    var result = context.Games.SingleOrDefault(g => g.GameID == update.GameID);
    if (result != null)
    {
        result = update;
        context.SaveChanges();
    }
    return Created("", result);
}

And this is the code I use in Angular:

url:string;
constructor(private _http: HttpClient) {
    this.url = "https://localhost:44359/api/v1/"
};

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

Edit 1: I do have a list of GET-requests, which all work just fine. It's only the PUT-request that fails.

1
  • Do your other ASP.Net controllers successfully invoke Angular? Q: Have you tried using RouteDebugger? Commented Nov 10, 2018 at 23:59

3 Answers 3

9

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 !!

Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

Yup. result = update doesn't do what you think it does.
I did this, but it still won't call when I try out the put-request. I have added a breakpoint on updateGame() and attempt a put-request via postman, but it never triggers.
As @Nkosi suggests, it may not do what you expect it should. Also check out learn.microsoft.com/en-us/aspnet/core/tutorials/…, there is an PUT example.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.