Share via

Using IHttpActionResult Put Verb

Ronald Rex 1,671 Reputation points
2021-09-22T21:39:11.887+00:00

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 | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

Developer technologies | ASP.NET Core | Other
Developer technologies | ASP.NET Core | ASP.NET API

Answer accepted by question author

Lan Huang-MSFT 30,211 Reputation points Microsoft External Staff
2021-09-23T07:37:43.013+00:00

Hi @Ronald Rex ,
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

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.