Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

How to post parameter from body [FromBody] and retrive objects in a in WebAPI?

$
0
0

How to post parameter to a body and get the object in a list.Now i am able to retrieve the data using this post method in my controller with parameter appended to the URI

Accounts Controller

    [HttpPost]
    [ActionName("mini")]
    public List<MiniStatement> GetMiniStatement(string accountNumber)
    {
        var miniState = BusinessLayer.Api.AccountHolderApi.GetMiniStatement(accountNumber);
        return miniState.ToList();
    }

But how do i pass the param [FromBody] and retrive the data in a List?

MiniStatementCLass

 public  class MiniStatement
{
    public string AccountNumber { get; set; }
    public string TranDate { get; set; }
    public string Trans { get; set; }
    public decimal Amount { get; set; }
}

GetMiniStatement Method in DB Layer

public static List<MiniStatement> GetMiniStatement(string accountNumber)
    {
        List<MiniStatement> resultList = new List<MiniStatement>();
        using (var conn = new NpgsqlConnection("Server=localhost;UserId = postgres; " + "Password = pes; Database = pmc;"))
        {
            conn.Open();
            using (var command = new NpgsqlCommand("SELECT * FROM sms.dep_mini_statement(@AccountNumber);", conn))
            {
                command.Parameters.AddWithValue("@AccountNumber", accountNumber);

                using (var dr = command.ExecuteReader())
                {

                    while (dr.Read())
                    {
                        var result = new MiniStatement
                        {
                            AccountNumber = accountNumber,
                            TranDate = (string)dr[0],
                            Trans = (string)dr[1],
                            Amount = (decimal)dr[2]
                        };
                        resultList.Add(result);
                    }

                }
            }

        }
        return resultList;

    }

And the Route

config.Routes.MapHttpRoute("MobileBankingApi", "v1/{controller}/{action}");




Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>