为什么没有查询参数传递给我的NancyFX模块? [英] Why are no query parameters being passed to my NancyFX module?
问题描述
我运行我的应用程序内部的自托管NancyFX Web服务器。现在我有一个模块托管
I am running a self-hosted NancyFX web server inside of my application. Right now I have one 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 am trying to test it with the 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 =>
{
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());
}
在这个测试调试和断点放在返回Response.AsJson(0);
,我检查参数对象,并注意到 parameters.Count
为零,所有3个值都为空
When this test is debugged and a breakpoint is put on return Response.AsJson(0);
, I inspected the parameters object and noticed that parameters.Count
is zero, and all 3 values are null.
我在做什么错误
编辑:当我打开该端点在网络浏览器时,发生同样的问题。我得到0的结果发回给我的浏览器,但在调试的时候我看到没有查询字符串参数我指定已被NancyFX认可。
When I bring up this endpoint in the web browser, the same issue occurs. I get a result of 0 sent back to my browser, but when debugging I see that no query string parameters I specify have been recognized by NancyFX.
推荐答案
的参数
参数的拉姆达包含您在你的获取[/查询] $捕获的路由参数C $ C>。在这种情况下,没有什么。见@对于有一些例子thecodejunkie的评论。
The parameters
argument to your lambda contains the route parameters you captured in the in your Get["/query"]
. In this case nothing. See @thecodejunkie's comment for an example where there is something.
要得到查询paramters使用 Request.Query
。这也是一个动态的,将包含任何查询参数是在请求中。像这样:
To get to the query paramters use Request.Query
. That's also a dynamic and will contain whatever query parameters was in the request. Like 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 with your tests too.
这篇关于为什么没有查询参数传递给我的NancyFX模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!