如何将多个参数传递给 DELETE 请求 C# Rest API

How to pass multiple parameters to DELETE request C# Rest API

我需要向 API DELETE 请求传递 3 个参数。这是我尝试过的代码。

任务模型

public class TaskModel
{
    public int DeveloperID { get; set; }
    public int ProjectID { get; set; }
    public string WorkDate { get; set; }
}

这是一个控制器class。叫做 TaskController

    [Route("api/Task")]
    public void Delete(TaskModel value)
    {
        TaskPersistent tp = new TaskPersistent();
        tp.deleteTask(value);
    }

这是TaskPersistent.class

public void deleteTask(TaskModel task)
{
    try
    {
        string sqlString = "DELETE from devproj WHERE (DeveloperID, ProjectID, WorkDate) =  VALUES ('" + task.DeveloperID + "', '" + task.ProjectID + "', '" + task.WorkDate + "')"; // System.NullReferenceException throw
        MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(sqlString, conn);
        cmd.ExecuteNonQuery();
        long x = cmd.LastInsertedId;
    }
    catch (MySqlException x)
    {
        int errr = x.Number;

        Console.WriteLine(errr);
    }
}

我像这样使用 ARC 休息客户端使用此 API,http://localhost:2731/api/Task?DeveloperID=1&ProjectID=2&WorkDate="2018-03-14" 但是当我像这样传递参数时,API 抛出异常:'System.NullReferenceException' in DeleteAPI.dll(我在我的代码中注释了错误发生行)。我在这里做错了什么。

您需要使用此 API,方法是将您的 TaskModel 作为模型活页夹的正文发布,以便按照您设置 Delete 控制器操作的方式工作。或者,将 Delete 方法参数更改为 int developerId, int projectId, DateTime workDate.

    [Route("api/Task")]
    public void Delete(int developerId, int projectId, DateTime workDate)
    {
        var taskModel = new TaskModel
        {
            DeveloperId = developerId,
            ProjectID = projectId,
            WorkDate = workDate
        };
        TaskPersistent tp = new TaskPersistent();
        tp.deleteTask(taskModel);
    }
string sqlString = "DELETE from devproj WHERE (DeveloperID, ProjectID, WorkDate) =  VALUES ('" + task.DeveloperID + "', '" + task.ProjectID + "', '" + task.WorkDate + "')"; // System.NullReferenceException throw

此查询无效。 SQL删除是:

string sqlString = $"DELETE from devproj WHERE DeveloperID = {task.DeveloperID} AND ProjectID = {task.ProjectID} AND WorkDate = {task.WorkDate}";

并且不要忘记在执行查询之前检查模型中的空值。