3

Nancy を調べ始めたばかりで、Tekpub の Sinatra ビデオ (Nancy の元になっているもの) を使用して、何ができるかを確認していました。ビデオでデモンストレーションされたことの 1 つは、要求情報 (要求メソッド、要求パスなど) をブラウザーに出力することでした。ASP.Net Web フォームを使用すると、Request オブジェクトでその情報を取得できますが、Nancy でこれを行う方法を示すドキュメントには何も表示されませんでした。Nancy.Request オブジェクトに Headers フィールドがあることは知っていますが、探していたすべての情報が得られるわけではありません。以下は、C# と Nancy に変換したい元の Sinatra コードです。

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
4

1 に答える 1

18

このようなことを意味しますか?

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

これにより、次のような出力が得られます。

{
   "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

于 2013-06-08T18:36:12.670 に答える