Scott Harrison - The golden apples
Henry Parker's Robot Wars

ASP.Net, ADO, MS Access
C# How to bind parameters into a c# class for Web Api

C# How to bind parameters into a c# class for Web Api

(10 hits)
Overview: Hits=7 This code snippet demonstrates how to call a web api get using Postman and bind the parameters into a parameter class filter. The webapi receives the query string parameters and parameterizes them into property fields using the class setters. This is an amazing feature of C# suggesting that by inference the properties can be discovered and the properties bound. In a previous article, I demonstrated how to use reflections to expose the class and a custom Mapper class to assign the properties values.

Hits=4 The web api controller is a file named "TimeAndAttendanceController" and by convention the path is api/TimeAndAttendance with a route defined as "TAViews". I pass the field names of the filterTimeAndAttendance class as a query string parameters. I like classes as parameters because the parameter list can become large.

POSTMAN

   
http://localhost:61612/api/TimeAndAttendance/TAViews?StartDate=10/1/2018&EndDate=10/14/2018

FilterTimeAndAttendance Class

public class FilterTimeAndAttendance   {
       public DateTime StartDate { get; set; }
       public DateTime EndDate { get; set; }
   }

POSTMAN 2 (another example of binding)

http://localhost:61612/api/ChartOfAccount/CoaViews?AccountIds=3&AccountIds=4&AccountIds=5
 public class FilterChartOfAccount
   {
       public long[] AccountIds { get; set; }

   }

TimeAndAttendance Web Api Controller

   [HttpGet]
   [Route("TAViews")]
           public List<TimeAndAttendanceView> Get([FromQuery] FilterTimeAndAttendance filter)
       {
           TimeAndAttendanceModule taMod = new TimeAndAttendanceModule();
           List<TimeAndAttendanceView> views = taMod.TimeAndAttendance.Query().GetTimeAndAttendanceViewsByDate(filter.StartDate, filter.EndDate);
           return views;
       }
....

...<<<Register to correspond>>> ...

Members : 152
Name:
Email:

Register to View

Help