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

(111 hits)
Overview: Hits=48 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=44 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.



FilterTimeAndAttendance Class

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

POSTMAN 2 (another example of binding)

 public class FilterChartOfAccount
       public long[] AccountIds { get; set; }


TimeAndAttendance Web Api Controller

           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

Register to View