Can't get HTTP PUT-request to work in Asp.Net Core












1














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.










share|improve this question
























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
















1














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.










share|improve this question
























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














1












1








1


1





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.










share|improve this question















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.







c# angular rest asp.net-core asp.net-core-routing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 12:43

























asked Nov 10 at 23:48









MyNameIsGuzse

299




299












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


















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
















Do your other ASP.Net controllers successfully invoke Angular? Q: Have you tried using RouteDebugger?
– paulsm4
Nov 10 at 23:59




Do your other ASP.Net controllers successfully invoke Angular? Q: Have you tried using RouteDebugger?
– paulsm4
Nov 10 at 23:59












3 Answers
3






active

oldest

votes


















1














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.






share|improve this answer





















  • Yup. result = update doesn't do what you think it does.
    – Gabriel Luci
    Nov 11 at 1:07










  • 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.
    – MyNameIsGuzse
    Nov 11 at 12:42










  • As @Nkosi suggests, it may not do what you expect it should. Also check out docs.microsoft.com/en-us/aspnet/core/tutorials/…, there is an PUT example.
    – Andre Artus
    Nov 11 at 12:54



















1














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






share|improve this answer





























    0














    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






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244551%2fcant-get-http-put-request-to-work-in-asp-net-core%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      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.






      share|improve this answer





















      • Yup. result = update doesn't do what you think it does.
        – Gabriel Luci
        Nov 11 at 1:07










      • 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.
        – MyNameIsGuzse
        Nov 11 at 12:42










      • As @Nkosi suggests, it may not do what you expect it should. Also check out docs.microsoft.com/en-us/aspnet/core/tutorials/…, there is an PUT example.
        – Andre Artus
        Nov 11 at 12:54
















      1














      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.






      share|improve this answer





















      • Yup. result = update doesn't do what you think it does.
        – Gabriel Luci
        Nov 11 at 1:07










      • 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.
        – MyNameIsGuzse
        Nov 11 at 12:42










      • As @Nkosi suggests, it may not do what you expect it should. Also check out docs.microsoft.com/en-us/aspnet/core/tutorials/…, there is an PUT example.
        – Andre Artus
        Nov 11 at 12:54














      1












      1








      1






      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.






      share|improve this answer












      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.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 11 at 0:10









      Nkosi

      109k16116184




      109k16116184












      • Yup. result = update doesn't do what you think it does.
        – Gabriel Luci
        Nov 11 at 1:07










      • 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.
        – MyNameIsGuzse
        Nov 11 at 12:42










      • As @Nkosi suggests, it may not do what you expect it should. Also check out docs.microsoft.com/en-us/aspnet/core/tutorials/…, there is an PUT example.
        – Andre Artus
        Nov 11 at 12:54


















      • Yup. result = update doesn't do what you think it does.
        – Gabriel Luci
        Nov 11 at 1:07










      • 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.
        – MyNameIsGuzse
        Nov 11 at 12:42










      • As @Nkosi suggests, it may not do what you expect it should. Also check out docs.microsoft.com/en-us/aspnet/core/tutorials/…, there is an PUT example.
        – Andre Artus
        Nov 11 at 12:54
















      Yup. result = update doesn't do what you think it does.
      – Gabriel Luci
      Nov 11 at 1:07




      Yup. result = update doesn't do what you think it does.
      – Gabriel Luci
      Nov 11 at 1:07












      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.
      – MyNameIsGuzse
      Nov 11 at 12:42




      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.
      – MyNameIsGuzse
      Nov 11 at 12:42












      As @Nkosi suggests, it may not do what you expect it should. Also check out docs.microsoft.com/en-us/aspnet/core/tutorials/…, there is an PUT example.
      – Andre Artus
      Nov 11 at 12:54




      As @Nkosi suggests, it may not do what you expect it should. Also check out docs.microsoft.com/en-us/aspnet/core/tutorials/…, there is an PUT example.
      – Andre Artus
      Nov 11 at 12:54













      1














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






      share|improve this answer


























        1














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






        share|improve this answer
























          1












          1








          1






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






          share|improve this answer












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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 5:09









          Rahul

          9631315




          9631315























              0














              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






              share|improve this answer


























                0














                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






                share|improve this answer
























                  0












                  0








                  0






                  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






                  share|improve this answer












                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 2:10









                  Md. Abdul Alim

                  349215




                  349215






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244551%2fcant-get-http-put-request-to-work-in-asp-net-core%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Hercules Kyvelos

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud