question

RonaldRex-2335 avatar image
0 Votes"
RonaldRex-2335 asked RonaldRex-2335 commented

Using IHttpActionResult Put Verb

I was wondering where does my client calling ajax get the ID to send to the IHttpActionResult Put Verb Action Method? I am accustomed to calling the api with the ID in the url like so /api/Product/id



 function productUpdate(product) {
             $.ajax({
                 url: "/api/Product",
                 type: 'PUT',
                 contentType:
                     "application/json;charset=utf-8",
                 data: JSON.stringify(product),
                 success: function (product) {
                   /*  productUpdateSuccess(product);*/
                 },
                 error: function (request, message, error) {
                     handleException(request, message, error);
                 }
             });
         }
    
    
    
    
    
 public IHttpActionResult Put(int id, Product product)
     {
       IHttpActionResult ret = null;
    
       if (Update(product))
       {
         ret = Ok(product);
       }
       else
       {
         ret = NotFound();
       }
    
       return ret;
     }


sql-server-generaldotnet-aspnet-generaldotnet-aspnet-core-webapidotnet-aspnet-webapi
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Why did you tagged your post with sql-server-general; there is no relation to?

1 Vote 1 ·

I tagged my post with sql-server-general because it is a very common architecture for developers to use web api's to retrieve data from sql server. Please give me some feedback on my reply. Thanks !!!






0 Votes 0 ·

1 Answer

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

Hi @RonaldRex-2335,
If you want to get the ID to be sent to IHttpActionResult Put Verb Action Method,
You can get the id directly after the url:

 url: '/api/product?id=' + id,

I make a simple example:

 $(function () {
             //for testing purpose
             var id = 5;
             var product = { "pname": "p001" };
             productUpdate(id, product);
     })
      function productUpdate(id, product) {
              $.ajax({
                  url: '/api/product?id=' + id,
                  type: 'PUT',
                  contentType:
                      "application/json;charset=utf-8",
                  data: JSON.stringify(product),
                  success: function (product) {
                    /*  productUpdateSuccess(product);*/
                  },
                  error: function (request, message, error) {
                      handleException(request, message, error);
                  }
              });
          }


 public IHttpActionResult Put(int id, Product product)
         {
             IHttpActionResult ret = null;
    
             if (Update(product))
             {
                 ret = Ok(product);
             }
             else
             {
                 ret = NotFound();
             }
    
             return ret;
         }



 public class Product
     {
         public string pname { get; set; }
     }


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Lan Huang


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.