Why are no query parameters being passed to my NancyFX module?

爱⌒轻易说出口 提交于 2019-12-18 19:42:11

问题


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());
    }

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.

What am I doing incorrectly?

Edit: 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.


回答1:


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.

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.




回答2:


You can let NancyFx's model binding take care of the url query string.

public class RequestObject 
{
    public string Start { get; set; }
    public string End { get; set; }
    public string Metrics { get; set; }
}

/query?start=2015-09-27&end=2015-10-27&metrics=loadtime

Get["/query"] = x =>
{
    var request = this.Bind<RequestObject>();
}


来源:https://stackoverflow.com/questions/18302891/why-are-no-query-parameters-being-passed-to-my-nancyfx-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!