asp.net web api advanced routing -
can 1 please me route trying setup
i have know if can , how go configuring route list so
localhost/api/device/1/commands/turnon
{hostname}/{api section}{i multiple types of devices diiferent commands}
so examples:
- localhost/api/device/1/commands/turnoff
- localhost/api/device/1/commands/turnon
- localhost/api/device/1/info/ (will return info on device)
- localhost/api/device/1/info/name (will return name of device)
- localhost/api/device/1/commands/display/1 (will display 1 on device)
the above random examples of things want do.
but underlying structure correct. want have multiple devices device specific commands , options
i don't know how portray in controller? think along long of controller each device under device have different sections command, info ,etc under each 1 of woudl have action methods.
but dont know how that.
any apperecited:
also better practice:
[httpget] public httpstatuscode arm() { return httpstatuscode.ok; } [httpget] public httpstatuscode stayarm() { return httpstatuscode.ok; } [httpget] public httpstatuscode disarm() { return httpstatuscode.ok; }
or:
[httpget] public httpstatuscode command(string command) { switch (command) { case "arm": { } break; case "disarm": { } break; } return httpstatuscode.ok; }
method 1 better practice. makes code more readable, maintainable , testable.
you wont have controller each device. should have 1 controller queries database or service device , performs action on device.
you can prefix controller actions following in order parameters route:
[httpget] [route("device/{deviceid}/commands/turnoff")] public httpstatuscode turnoff(int deviceid) { // 1. find device deviceid // 2. turn device off return httpstatuscode.ok; }
Comments
Post a Comment