c# - 具有多个参数 HttpPut 的 ASP.NET Web 服务
问题描述
我正在尝试在我的数据库中插入值,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)。
现在它按预期工作!
推荐阅读
- mongodb - 如何使用 MongoDB 对对象进行分组并显示查询中的所有预期字段
- modulenotfounderror - 找不到模块:无法解决“firebase”错误
- r - 调查分类数据的分组条形图
- javascript - 在 useEffect 挂钩内测试路由器
- node.js - NodeJs复制文件操作不允许错误
- asp.net-core - ASP.NET Core 3.1 图像 URL 错误代码 302 重定向
- flutter - 使用底部导航栏颤动来导航页面
- c# - 需要有关基于内存拉取的队列库的建议
- plot - 八度图例符号大小与绘图大小不同
- string - 第一个字符串以球拍语言中的第二个字符串开头