Reputation: 4509
I just started looking at Nancy and I was using a Sinatra Video (what Nancy is based on) from Tekpub, to see what it could do. One of the things demonstrated in the video was outputting the request information back to the browser (request method, request path, etc...). When I use ASP.Net Web Forms I can get to that information in the Request object, but I didn't see anything in the documentation that showed me how to do this in Nancy. I know there is a Headers field in the Nancy.Request object, but it didn't give me all the information I was looking for. Below is the original Sinatra Code that I want to convert to C# and Nancy:
class HelloWorld
def call(env)
out = ""
env.keys.each {|key| out+="#{key}=#{env[key]}"}
["200",{"Content-Type" => "text/plain"}, out]
end
end
run HelloWorld.new
Upvotes: 3
Views: 12033
Reputation: 18804
You mean something like this?
Get["/test"] = _ =>
{
var responseThing = new
{
this.Request.Headers,
this.Request.Query,
this.Request.Form,
this.Request.Session,
this.Request.Method,
this.Request.Url,
this.Request.Path
};
return Response.AsJson(responseThing);
};
This would give you an output like:
{
"Form":{
},
"Headers":[
{
"Key":"Cache-Control",
"Value":[
"max-age=0"
]
},
{
"Key":"Connection",
"Value":[
"keep-alive"
]
},
{
"Key":"Accept",
"Value":[
"text/html;q=1",
"application/xhtml+xml;q=1",
"application/xml;q=0.9",
"*/*;q=0.8"
]
},
{
"Key":"Accept-Encoding",
"Value":[
"gzip,deflate,sdch"
]
},
{
"Key":"Accept-Language",
"Value":[
"en-US,en;q=0.8"
]
},
{
"Key":"Host",
"Value":[
"localhost:2234"
]
},
{
"Key":"User-Agent",
"Value":[
"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36"
]
}
],
"Method":"GET",
"Path":"/test",
"Query":{
"23423":"fweew"
},
"Session":[
],
"Url":{
"BasePath":null,
"Fragment":"",
"HostName":"localhost:2234",
"IsSecure":false,
"Path":"/test",
"Port":null,
"Query":"23423=fweew",
"Scheme":"http",
"SiteBase":"http://localhost:2234"
}
}
You can also get the Owin Environment Variables as described in the wiki here
https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owin#accessing-owin-environment-variables
Upvotes: 18