Loading...

Web api call with param object[] as parametar


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/

- - Source

Answers

answered 1 week ago Doh09 #1

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://docs.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.

comments powered by Disqus