Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有多参数HttpPut的ASP.NET web服务_C#_Asp.net_Http Post_Webservice Client_Http Post Vars - Fatal编程技术网

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个坏网关