1

我正在尝试在我的数据库中插入值,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();
    }
}

有没有办法可以做到这一点?

4

2 回答 2

1

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

[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)。

现在它按预期工作!

于 2018-08-23T09:14:29.530 回答
0

定义属性路由。

[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();
    }
}
于 2018-08-23T09:17:36.187 回答