简体   繁体   English

具有多个参数HttpPut的ASP.NET Web服务

[英]ASP.NET web-service with multiple parameters HttpPut

I am trying to insert values in my database using HttpPut but I can't get it to work for multiple parameters. 我正在尝试使用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();
    }
}

Is there a way I can achieve this? 有没有办法可以做到这一点?

Ok, I figured it out what I was doing wrong. 好的,我弄清楚了我做错了什么。

[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
        { }
    }
}

Adjusted code to this. 调整代码为此。

I was mistakenly running POST instead of PUT (arrgghgh). 我错误地运行POST而不是PUT(arrgghgh)。

Now it works as expected! 现在它可以正常工作了!

Define attribute route. 定义属性路由。

[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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET/jQuery:将JSON发布到Web服务 - ASP.NET / jQuery : post JSON to web-service ASP.NET MVC 2和FormsAuthentication的Web服务身份验证 - Web-service authentication from ASP.NET MVC 2 and FormsAuthentication 在Asp.net Mvc 5中的HttpPut? - HttpPut in Asp.net Mvc 5? 无法使用 asp.net web 服务将 ajax 标头值传递给我的 web 服务方法 - Not able to pass ajax headers value to my web-service method using asp.net web service ASP.NET:为什么我不能在一个页面中两次调用Web服务? - ASP.NET: Why can't I call a web-service twice in one page? 来自外部HTML页面的asp.net Web服务调用未定义错误 - Undefined Error on asp.net web-service call from external HTML page ASP.NET 核心 5,带有来自正文的参数的 HttpPut 请求始终返回 Http 400 错误请求 - ASP.NET Core 5, HttpPut request with parameters from body always returns Http 400 Bad request 使用HttpPut时,ASP.NET Web API始终返回405“不允许的方法”状态代码 - ASP.NET Web API keeps returning a 405 'Method Not Allowed' status code when using HttpPut 在ASP.Net Web服务的构造函数中使用带有参数的自定义对象 - Using custom object with parameters in the constructor in ASP.Net web service 如何在asp.net WEB API中传递多个querystring参数 - how to pass multiple querystring parameters in asp.net WEB API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM