angularjs - How to access multiple HTTP GET actions in same controller? -
here web api controller:
[httpget] public httpresponsemessage sensordata(string id) { try { responds data = accessremote.getdatafromdevice(id); dataresponds dataresponds = data.returndatrequest[0]; return request.createresponse(httpstatuscode.ok, dataresponds); } catch (exception ex) { return request.createresponse(httpstatuscode.forbidden, ex.message); } } [httpget] public httpresponsemessage getlogs(string getrecordsbyid) { try { iqueryable<sensorinfo> data = sensorresultsrepos.get(); httpresponsemessage response = request.createresponse(httpstatuscode.ok, data); return response; } catch (exception) { throw; } }
here resource angularjs definition:
(function () { "use strict"; angular.module("sensormanagement").factory("sensorresource", ["$resource", sensorresource]) function sensorresource($resource) { return $resource("http://localhost:1234/api/somedata/:id"); } }());
here web api route:
public static void register(httpconfiguration config) { // web api configuration , services // web api routes config.maphttpattributeroutes(); config.routes.maphttproute( "defaultapisensor", "api/{controller}/{id}", new { controller = "sensor", id = routeparameter.optional } ); config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } ); }
at point need access sensordata action or getlogs action using sensorresource service.
the problem thye both http , both have 1 parameter.
how can make call sensordata , getlogs if in 1 controller , have sane http type?
thank in advance!
you can use attribute routing solve problem. see this link more information regarding topic.
[httpget] [route("sensor/{id}")] public httpresponsemessage sensordata(string id) { try { responds data = accessremote.getdatafromdevice(id); dataresponds dataresponds = data.returndatrequest[0]; return request.createresponse(httpstatuscode.ok, dataresponds); } catch (exception ex) { return request.createresponse(httpstatuscode.forbidden, ex.message); } } [httpget] [route("logs/{getrecordsbyid}")] public httpresponsemessage getlogs(string getrecordsbyid) { try { iqueryable<sensorinfo> data = sensorresultsrepos.get(); httpresponsemessage response = request.createresponse(httpstatuscode.ok, data); return response; } catch (exception) { throw; } }
Comments
Post a Comment