Http监听HttpListener接收请求的Nancy框架

1.Nuget下载Nancy和Nancy.Hosting.Self,两个都是2.0.0版本。

1.开启本地被http请求端口

try
{
  NancyHost host = new NancyHost(new Uri("http://localhost:59355"));
  HostConfiguration hostConfig = new HostConfiguration();
  hostConfig.UrlReservations = new UrlReservations { CreateAutomatically = true };
  host.Start();
}
catch (Exception ex)
{
   Console.WriteLine(ex.Message);
}

2.编写请求的路由,添加类继承 NancyModule

public class MyModule : NancyModule
{
  public MyModule()
  {
    Get("/", r => "Nancy running on ASP.NET");
  }
} 

PS:

自定义 请求方 的http请求路由,如下

Get("/analysis/result/{indexID:int}/{dimentionID:int}", args =>
{
          var indexID = args.indexID;
          var dimentionID = args.dimentionID;
          return indexID + "--" + dimentionID;
});

 

 或者自定义请求参数,如下

Get("/analysis/result", args =>
{
        var indexID = this.Request.Query["indexID"];                       
        var dimentionID = this.Request.Query["dimentionID"];                
        return indexID + "--" + dimentionID;
});    

 

posted @ 2021-01-12 12:30  超级驼鹿  阅读(338)  评论(0编辑  收藏  举报
/*