Search code examples
c#asp.nethttp-postwebservice-clienthttp-post-vars

ASP.NET web-service with multiple parameters HttpPut


I am trying to insert values in my database using HttpPut but I can't get it to work for multiple parameters.

// PUT: api/Orders/CustomerID/TableID
[HttpPut("{CustomerID,TableID}")]
public async Task<IActionResult> PutOrder([FromRoute] string CustomerID, [FromRoute] string TableID)
{
    using (MySqlConnection connection = new MySqlConnection(ConnectionString))
    {
        try
        {
            string s = "INSERT INTO orders (CustomerID, TableID) VALUES (@CustomerID, @TableID)";
            MySqlCommand command = new MySqlCommand(s, connection);
            command.Parameters.AddWithValue("@CustomerID", CustomerID);
            command.Parameters.AddWithValue("@TableID", TableID);
            connection.Open();

            command.ExecuteNonQuery();
        }
        catch
        { }
        return NoContent();
    }
}

Is there a way I can achieve this?


Solution

  • Ok, I figured it out what I was doing wrong.

    [HttpPut("{CustomerID}/{TableID}")]
    public void PutOrder([FromRoute] string CustomerID, [FromRoute] string TableID)
    {
        using (MySqlConnection connection = new MySqlConnection(ConnectionString))
        {
            try
            {
                string s = "INSERT INTO orders (CustomerID, TableID) VALUES (@CustomerID, @TableID)";
                MySqlCommand command = new MySqlCommand(s, connection);
                command.Parameters.AddWithValue("@CustomerID", CustomerID);
                command.Parameters.AddWithValue("@TableID", TableID);
                connection.Open();
    
                command.ExecuteNonQuery();
            }
            catch
            { }
        }
    }
    

    Adjusted code to this.

    I was mistakenly running POST instead of PUT (arrgghgh).

    Now it works as expected!