在NancyFx中获取URL参数 [英] Get url parameters in NancyFx
问题描述
我正在使用NancyFx构建Web API,但是从URL获取参数时遇到了一些问题.
I am using NancyFx to build a web API, but I am facing some problems when getting parameters from the URL.
我需要将请求.../consumptions/hourly?from=1402012800000&tags=%171,1342%5D&to=1402099199000
发送到API,并捕获以下参数的值:粒度,从,标记和到.我尝试了几种方法,但均无效果.例如,我尝试过
I need to send, to the API, the request .../consumptions/hourly?from=1402012800000&tags=%171,1342%5D&to=1402099199000
and catch the value of the parameters: granularity, from, tags and to. I tried several approches and none worked. I tried, for example,
Get["consumptions/{granularity}?from={from}&tags={tags}&to={to}"] = x =>
{
...
}
我该怎么做?
路易斯·桑托斯
推荐答案
您试图从URL中获得两件事.一个是路径hourly
的一部分-另一个是查询字符串中的参数-即from
和to
的值.
There are 2 things that you are trying to get from the URL. One is a part of the path hourly
- and the other is the parameters in the query string - namely the values for from
and to
.
您可以通过处理程序的参数到达路径的一部分-在您的示例中为x
.
You can get to the part of the path through the parameter to the handler - the x
in your example.
您可以通过NancyModule
上的Request
进入查询字符串.
You can get to the query string through the Request
which is accessible on the NancyModule
.
将其放入代码中
Get["consumptions/{granularity}"] = x =>
{
var granularity = x.granularity;
var from = this.Request.Query["from"];
var to = this.Request.Query["to"];
}
变量granularity
. from
和to
都是dynamic
,您可能需要将它们转换为所需的任何类型.
The variables granularity
. from
, and to
are all dynamic
, and you may need to convert them to whatever type you want.
这篇关于在NancyFx中获取URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!