ASP.NET 具有多个参数的 Web 服务 HttpPut

ASP.NET web-service with multiple parameters HttpPut

我正在尝试使用 HttpPut 在我的数据库中插入值,但我无法让它为多个参数工作。

// 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();
    }
}

有什么办法可以实现吗?

好的,我知道我做错了什么了。

[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
        { }
    }
}

为此调整了代码。

我错误地 运行 POST 而不是 PUT (arrgghgh).

现在它按预期工作了!

定义属性路由。

[HttpPut]
[Route("api/Orders/{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();
    }
}