将 2 个参数传递给 Fiddler 中的 PUT 请求的方法

Method to pass 2 parameters to a PUT request in Fiddler

我正在尝试更新 table 中的单个记录。代码如下:

public void UpdateStudent(int id, int studentClass)
{
    SqlConnection myConnection = new SqlConnection();

    myConnection.ConnectionString = @"Data Source=PALLAVI-PC\SQLEXPRESS;Initial Catalog=StudentDB;Integrated Security=True;MultipleActiveResultSets=True;";

    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.CommandType = CommandType.Text;
    sqlCmd.CommandText = "update Tbl_Students set Class="+ studentClass+" where Roll_Number=" + id + ";";

    sqlCmd.Connection = myConnection;

    myConnection.Open();

    int rowUpdated = sqlCmd.ExecuteNonQuery();
    myConnection.Close();
}

如何将 id 和 Class 都传递给 fiddler 中的 URL?

您可以使用 WCF REST 服务来实现。 实施以下方法,你可以通过提琴手做你想做的事:

[WebInvoke(Method = "PUT", UriTemplate = "api/student/{id}/{studentclass}")]
[OperationContract]
void UpdateEmployee(int id, int studentclass);

有关 RESTFUL WCF 服务的更多详细信息,请参阅 link

感谢您的帮助。我能够通过稍微更改代码来解决问题。 代码如下:

    [HttpPut]
    [ActionName("UpdateStudent")]
    public void UpdateStudent(int id, [FromBody]Tbl_Students student)
    {
        SqlConnection myConnection = new SqlConnection();

        myConnection.ConnectionString = @"Data Source=PALLAVI-PC\SQLEXPRESS;Initial Catalog=StudentDB;Integrated Security=True;MultipleActiveResultSets=True;";

        SqlCommand sqlCmd = new SqlCommand();
        sqlCmd.CommandType = CommandType.Text;
        sqlCmd.CommandText = "update Tbl_Students set Class=" + student.Class + " where Roll_Number=" + id + ";";

        sqlCmd.Connection = myConnection;

        myConnection.Open();

        int rowUpdated = sqlCmd.ExecuteNonQuery();
        myConnection.Close();
    }

在 Fiddler 中,我将 id 作为 URL 的一部分传递,将 studentClass 作为 JSON 传递到请求正文中。我还添加了我之前忘记的 Content-Type:application/json 。我添加了下面的图片: