0

Is it possible to call api with params object[] as parameter

    [HttpGet("{procName}/{parametarList}")]
    public ActionResult<string> Get(string procName , params object[] parametarList)
    {
        string JSONString = string.Empty;

        using (var ado = new ADO())
        {

            var ds = ado.ExecuteAndReturnDS("execute " + procName " @0, @1, @2,@3,@4,@5", parametarList);

            JSONString = JsonConvert.SerializeObject(ds.Tables[0]);

            return new JsonResult(JSONString);

        }
    }

 public DataSet ExecuteAndReturnDS(string upit, params object[] parametri)
    {
        try
        {
            _Conn();

            command = new SqlCommand(upit, _Conn);

            command.CommandTimeout = 200;

            _ds = new DataSet();
            _sqlda = new SqlDataAdapter(command);

            for (int i = 0; i <= parametri.Count() - 1; i++)
            {
                if (parametri[i] == null)
                {
                    command.Parameters.AddWithValue("@" + i, DBNull.Value);
                }
                else
                {
                    command.Parameters.AddWithValue("@" + i, parametri[i]);
                }
            }

            _sqlda.Fill(_ds);

            if (_Conn.State == ConnectionState.Open)
            {
                _sqlda.Dispose();

                _Conn.Dispose();
                _Conn.Close();
            }

            return _ds;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

Like this /api/values/myProcedure/param1/param2/param3/param4/etc/

Jande
  • 1,695
  • 2
  • 20
  • 32
  • You have to add the parameters to a command. So you can use AddRange() like following : cmd.Parameters.AddRange(params) – jdweng Apr 15 '19 at 12:40
  • In fact you can have a problem if the list has many items. Check this https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers – Azzy Elvul Apr 15 '19 at 12:46

1 Answers1

0

I can see 2 ways you can do it.

The first one is to pass in the parameterlist as just a string and then split it into an array of objects in the method.

Here is an example of how you can easily split a string in C#: https://learn.microsoft.com/en-us/dotnet/csharp/how-to/parse-strings-using-split

The second is to use a POST request instead of a GET request, this would allow you to pass in more complicated objects.

Doh09
  • 2,324
  • 1
  • 16
  • 31
  • JSON would be the POST option I mention. I wouldn't recommend to send JSON in the URL as that quickly can grow to be less than ideal. Not even sure its possible. – Doh09 Apr 15 '19 at 12:47