简体   繁体   English

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

[英]How to pass multiple parameters to DELETE request C# Rest API

I need to pass 3 parameters to my API DELETE request. 我需要将3个参数传递给我的API DELETE请求。 Here is my code what I have try. 这是我尝试的代码。

TaskModel TaskModel

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

This a controller class. 这是一个控制器类。 called TaskController 叫做TaskController

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

This is TaskPersistent.class 这是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);
    }
}

I consume this API using ARC rest client like this, http://localhost:2731/api/Task?DeveloperID=1&ProjectID=2&WorkDate="2018-03-14" But when I pass the parameters like this,API thrown exception: 'System.NullReferenceException' in DeleteAPI.dll (I commented error occurred line in my code). 我使用ARC rest客户端使用此API,例如http://localhost:2731/api/Task?DeveloperID=1&ProjectID=2&WorkDate="2018-03-14"但是当我传递此类参数时,API抛出异常: 'System.NullReferenceException' in DeleteAPI.dll (我在我的代码行中注释了错误发生)。 What I did wrong here. 我在这里做错了。

You need to consume this API by posting your TaskModel as a body for the model binder to work with the way you have your Delete controller action set up. 您需要通过将TaskModel作为主体发布来使用此API,以使模型绑定程序与您设置Delete Controller操作的方式一起使用。 Alternatively, change the Delete method parameters to int developerId, int projectId, DateTime workDate . 或者,将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

This query don't work. 此查询不起作用。 SQL delete is : SQL删除是:

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

And don't forget to check for null value in your model before execute query. 并且不要忘记在执行查询之前检查模型中的空值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM