c# - Why are no query parameters being passed to my NancyFX module? -
i running self-hosted nancyfx web server inside of application. right have 1 module hosted:
public class metricsmodule : nancymodule { private istorageengine _storageengine; public metricsmodule(istorageengine storageengine) : base("/metrics") { _storageengine = storageengine; get["/list"] = parameters => { var metrics = _storageengine.getknownmetrics(); return response.asjson(metrics.toarray()); }; get["/query"] = parameters => { var rawstart = parameters.start; var rawend = parameters.end; var metrics = parameters.metrics; return response.asjson(0); }; } }
my bootstrapper class is:
public class overlookbootstrapper : defaultnancybootstrapper { private readonly istorageengine _storageengine; public overlookbootstrapper(istorageengine storageengine) { _storageengine = storageengine; } protected override void configureapplicationcontainer(tinyioccontainer container) { container.register(_storageengine); } }
i trying test following test:
[testinitialize] public void init() { _storageengine = new mock<istorageengine>(); var bootstrapper = new overlookbootstrapper(_storageengine.object); _browser = new browser(bootstrapper); } [testmethod] public void query_builds_correct_query_from_parameters() { var metric = new metric("device", "category", "name", "suffix"); var startdate = datetime.now; var enddate = datetime.now.addminutes(10); var path = "/metrics/query"; var response = _browser.get(path, => { with.httprequest(); with.query("start", startdate.tostring()); with.query("end", enddate.tostring()); with.query("metrics", metric.toparsablestring()); }); assert.areequal(httpstatuscode.ok, response.statuscode, "incorrect status code returned"); _storageengine.verify(x => x.executequery(it.is<query>(y => y.startdate == startdate)), times.once()); _storageengine.verify(x => x.executequery(it.is<query>(y => y.enddate == enddate)), times.once()); _storageengine.verify(x => x.executequery(it.is<query>(y => y.metrics.contains(metric))), times.once()); }
when test debugged , breakpoint put on return response.asjson(0);
, inspected parameters object , noticed parameters.count
zero, , 3 values null.
what doing incorrectly?
edit: when bring endpoint in web browser, same issue occurs. result of 0 sent browser, when debugging see no query string parameters specify have been recognized nancyfx.
the parameters
argument lambda contains route parameters captured in in get["/query"]
. in case nothing. see @thecodejunkie's comment example there something.
to query paramters use request.query
. that's dynamic , contain whatever query parameters in request. so:
get["/query"] = parameters => { var rawstart = request.query.start; var rawend = request.query.end; var metrics = request.query.metrics; return response.asjson(0); };
this should work tests too.