Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

nancy - Get url parameters in NancyFx

I am using NancyFx to build a web API, but I am facing some problems when getting parameters from the URL.

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 =>
{
    ...
}

How can I do this?

Luis Santos

question from:https://stackoverflow.com/questions/24189172/get-url-parameters-in-nancyfx

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

You can get to the part of the path through the parameter to the handler - the x in your example.

You can get to the query string through the Request which is accessible on the NancyModule.

To put this in code:

Get["consumptions/{granularity}"] = x =>
{
    var granularity = x.granularity;
    var from = this.Request.Query["from"];
    var to = this.Request.Query["to"];
}

The variables granularity. from, and to are all dynamic, and you may need to convert them to whatever type you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...