首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取Nancy请求信息

获取Nancy请求信息
EN

Stack Overflow用户
提问于 2013-06-09 01:46:23
回答 1查看 11.4K关注 0票数 3

我刚开始看Nancy,我使用了Tekpub的Sinatra Video ( Nancy基于它),看看它能做些什么。视频中演示的其中一件事是将请求信息输出回浏览器(请求方法、请求路径等)。当我使用ASP.Net Web Forms时,我可以在Request对象中获取该信息,但是在说明如何在Nancy中执行此操作的文档中,我没有看到任何内容。我知道在Nancy.Request对象中有一个Headers字段,但它没有给出我想要的所有信息。下面是我想要转换为C#和Nancy的原始Sinatra代码:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-09 02:36:13

你是说像这样的东西?

代码语言:javascript
复制
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);
};

这将为您提供类似以下内容的输出:

代码语言:javascript
复制
{
   "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"
   }
}

您还可以获取这里的wiki中描述的Owin环境变量

https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owin#accessing-owin-environment-variables

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17002179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档