1. 程式人生 > 實用技巧 >Http監聽HttpListener的Nancy框架

Http監聽HttpListener的Nancy框架

1.Nuget下載Nancy和Nancy.Hosting.Self,兩個都是2.0.0版本

1.開啟本地被http請求埠

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

2.編寫請求的路由,新增類繼承 NancyModule

  public class MyModule : NancyModule
    {
        
public IndexModule() { Get("/", x => "Hello World"); } }

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