Using IHttpActionResult Put Verb

Ronald Rex 1,666 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;
    }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,191 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,274 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,765 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
299 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,716 Reputation points Microsoft Vendor
    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

    0 comments No comments

0 additional answers

Sort by: Most helpful