C# 具有多参数HttpPut的ASP.NET web服务
我正在尝试使用C# 具有多参数HttpPut的ASP.NET web服务,c#,asp.net,http-post,webservice-client,http-post-vars,C#,Asp.net,Http Post,Webservice Client,Http Post Vars,我正在尝试使用HttpPut在数据库中插入值,但无法使其对多个参数起作用 // PUT: api/Orders/CustomerID/TableID [HttpPut("{CustomerID,TableID}")] public async Task<IActionResult> PutOrder([FromRoute] string CustomerID, [FromRoute] string TableID) { using (MySqlConnection conne
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();
}
}
//PUT:api/Orders/CustomerID/TableID
[HttpPut(“{CustomerID,TableID}”)]
公共异步任务PutOrder([FromRoute]字符串CustomerID,[FromRoute]字符串TableID)
{
使用(MySqlConnection=newmysqlconnection(ConnectionString))
{
尝试
{
string s=“插入订单(CustomerID,TableID)值(@CustomerID,@TableID)”;
MySqlCommand=newmysqlcommand(s,连接);
command.Parameters.AddWithValue(“@CustomerID”,CustomerID);
command.Parameters.AddWithValue(“@TableID”,TableID);
connection.Open();
command.ExecuteNonQuery();
}
抓住
{ }
返回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(啊哈)
现在它的工作如预期 定义属性路由
[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();
}
}
[HttpPut]
[路由(“api/Orders/{CustomerID}/{TableID}”)]
公共异步任务PutOrder([FromRoute]字符串CustomerID,[FromRoute]字符串TableID)
{
使用(MySqlConnection=newmysqlconnection(ConnectionString))
{
尝试
{
string s=“插入订单(CustomerID,TableID)值(@CustomerID,@TableID)”;
MySqlCommand=newmysqlcommand(s,连接);
command.Parameters.AddWithValue(“@CustomerID”,CustomerID);
command.Parameters.AddWithValue(“@TableID”,TableID);
connection.Open();
command.ExecuteNonQuery();
}
抓住
{ }
返回NoContent();
}
}
我无法让它工作
到底发生了什么?目前,我在尝试运行PUT/api/orders/tom/5时得到了502个坏网关