我尝试使用 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)。
现在它可以按预期工作了!
关于c# - 具有多个参数的 ASP.NET Web 服务 HttpPut,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51982018/