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

时间:2015-12-02 08:58:11

标签: c# asp.net asp.net-web-api2 fiddler

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

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?

2 个答案:

答案 0 :(得分:1)

您可以使用WCF REST服务执行此操作。 实施以下menthod,你可以做你想要的东西而不是提琴手:

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

请参阅link以获取有关RESTFUL WCF服务的更多详细信息。

答案 1 :(得分:0)

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

    [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传递给Request Body。我还添加了我之前忘记的Content-Type:application/json。我添加了以下图片:

enter image description here