LeeTeke.HttpServerLite.Hosting 1.1.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package LeeTeke.HttpServerLite.Hosting --version 1.1.2
                    
NuGet\Install-Package LeeTeke.HttpServerLite.Hosting -Version 1.1.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LeeTeke.HttpServerLite.Hosting" Version="1.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LeeTeke.HttpServerLite.Hosting" Version="1.1.2" />
                    
Directory.Packages.props
<PackageReference Include="LeeTeke.HttpServerLite.Hosting" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LeeTeke.HttpServerLite.Hosting --version 1.1.2
                    
#r "nuget: LeeTeke.HttpServerLite.Hosting, 1.1.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LeeTeke.HttpServerLite.Hosting@1.1.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LeeTeke.HttpServerLite.Hosting&version=1.1.2
                    
Install as a Cake Addin
#tool nuget:?package=LeeTeke.HttpServerLite.Hosting&version=1.1.2
                    
Install as a Cake Tool

LeeTeke.HttpServerLite.Hosting

· LeeTeke.HttpServerLite for Microsoft.Extensions.Hosting.

Nuget

NUGET

dotnet add package LeeTeke.HttpServerLite.Hosting --version 1.1.2

基本使用方法

初始化

using LeeTeke.HttpServerLite;
using LeeTeke.HttpServerLite.Hosting

  
            var _hosting = Host.CreateDefaultBuilder(args)
                .ConfigureHostConfiguration(configuration =>
                {
                    configuration.AddJsonFile("appsettings.json", false, true);
                    configuration.AddCommandLine(args);

                })
                .ConfigureServices((hosting, service) =>
                {
                    service.AddSingleton<TestRootController>();
                    service.AddSingleton<TestController>();

                })
                //使用 UseHttpServerLite 后会自动 将 HttpListenerBuilder 注册为 Singleton。 
                //手动配置
                //.UseHttpServerLite(new HttpApplicationOptions() { Port=81},HttpServerLiteConfigure)
                //使用配置文件配置版本
                .UseHttpServerLite(HttpServerLiteConfigure)
                .Build();

            _hosting.Start();
            _hosting.WaitForShutdown();



        static void HttpServerLiteConfigure(HostBuilderContext hosting, IServiceProvider services, HttpListenerBuilder httpBuilder)
        {
            //日志输出
            httpBuilder.HttpServerLiteLogTrigger += (sender, args) =>
            {
                if (args.Exception == null)
                {
                    Console.WriteLine($"info:{args.Msg}");
                }
                else
                {
                    Console.WriteLine($"error:{args.Msg}\t{args.Exception}");
                }
            };

            //直接映射,优先级最高
            httpBuilder.Map("/test", p => p.SendString("你好"));

            //所有路由发生前
            httpBuilder.BeforeRoute((context, next) =>
            {

                var url = context.Request.Url!.LocalPath;
                Console.WriteLine($"request:{url}");
                //拦截阻止
                if (url == "/stop")
                {
                    context.SendString("禁止访问");
                    return;
                }


                //使用HSTS
                //if (!context.HSTS())
                //    return;

                ////解决跨域问题
                //context.CorsAllowHeaders().CorsAllowMethods().CorsAllowOrigin();

                //继续下一步
                next();

            });

            //路由失败,无路由触发
            httpBuilder.AfterRouteFailure(context =>
            {
                var url = context.Request.Url!.LocalPath;
                Console.WriteLine($"no route:{url}");
                context.SendString($"不存在当前的地址:{url}");
            });

            //路由时发生异常捕获(log会捕捉非Task任务,如果是Task任务则只能在这里捕获。注意 async void 无法捕获。)
            httpBuilder.RouteExceptionFactory((context, ex) =>
            {
                var url = context.Request.Url!.LocalPath;
                Console.WriteLine($"route exception:{ex.Message}");
                context.Close(System.Net.HttpStatusCode.ServiceUnavailable);
            });
            //测试异常
            httpBuilder.Map("/ex", context =>
            {
                throw new Exception("thrwo exception !");
            });

            //使用Controller
            httpBuilder.ControllerAdd(services.GetRequiredService<TestRootController>());
            httpBuilder.ControllerAdd(services.GetRequiredService<TestController>());

            //提供了Vue文件的快速构建路由
            //参数输入基于RootPath位置
            //访问 www.xxx.com/vue/ 会直接运行 单页面模式 vue。
            httpBuilder.ControllerAdd(new VueHistoryModeRouter("/vue/") { UseCache_Lastmodified = true, UseGZip = true });


            //这里不用写Run了,服务会自动启动
            //httpBuilder.Run();
        }

HttpControllerBase 的基本使用

// Controller 使用
// 继承 HttpControllerBase

  using LeeTeke.HttpServerLite;

   //必须基于 HttpControllerBase
  public class TestController : HttpControllerBase
  {
    

      public TestController()
      {
          //这里填写路由地址;
          //默认地址为‘/’
          //此地址的含义为 www.xxx.com/testcontroll/
          base.RoutePrefix = "/testcontroll/";
      }

      //请注意 此方法 谨慎使用 async void 形式 ,若必须使用,则请注意在内部使用try catch;
      public override void BeforeRoute(HttpListenerContext listenerContext, Action<object[]?> next)
      {
          //这里是控制器内部的先行判断
          switch (listenerContext.Request.Url!.LocalPath)
          {
              case "/testcontroll/all/before":
                  //拦截
                  listenerContext.SendString("被拦截了");
                  break;
              case "/testcontroll/before":
                  //传参,传递的参数不要包含 HttpListenerContext,并且请按照相应路由方法的参数顺序进行传参(除去HttpListenerContext)。
                  next([DateTime.Now.ToString()]);
                  break;
              default:
                  //默认下一步,传参为空Array即可
                  next([]);
                  break;
          }


      }


      //1、方法必须是Public;
      //2、必须有 RouteAttribute,可以多个;
      //3、可以无参,但是http路由会在进入方法前结束http会话,并返回 httpstatuscode=200;
      //4、若有参数则首位参数必须是 HttpListenerContext listenerContext
      // www.xxx.com/testcontroll/ 路由到这里
      [Route("/")]
      public void Root(HttpListenerContext context)
      {
          context.SendString("这里是 TestController");
      }

      // www.xxx.com/testcontroll/a 路由到这里
      [LeeTeke.HttpServerLite.Route("a")]
      public void A(HttpListenerContext context)
      {
          context.SendString(context.Request.Url!.LocalPath);
      }

      // www.xxx.com/testcontroll/b 与 www.xxx.com/testcontroll/c路由到这里
      [LeeTeke.HttpServerLite.Route("b")]
      [LeeTeke.HttpServerLite.Route("c")]
      public void B(HttpListenerContext context)
      {
          context.SendString(context.Request.Url!.LocalPath);
      }

      // www.xxx.com/testcontroll/d 路由到这里,但是进入方法之前已被响应
      [LeeTeke.HttpServerLite.Route("d")]
      public void D()
      {
          Console.WriteLine("/testcontroll/d 发生了访问");
      }

      // www.xxx.com/testcontroll/all/ 以及 所有地址前缀为 www.xxx.com/testcontroll/all/ 都会路由到这里
      [LeeTeke.HttpServerLite.Route("all/", TakeOver =true)]
      public void All(HttpListenerContext context)
      {
          context.SendString(context.Request.Url!.LocalPath);
      }

      //多参数属性,如果在 BeforeRoute 方法里传递的参数个数多余本方法则会截取,若小于本方法,则不会进入此方法,并且http会做 501(NotImplemented)回应。
      // www.xxx.com/testcontroll/before路由到这里
      [LeeTeke.HttpServerLite.Route("before")]
      public void Before(HttpListenerContext context,string obj)
      {
          context.SendString(context.Request.Url!.LocalPath+"_"+obj);
      }


      //关于方法体里的异常说明
        //关于方法的异常捕获,此写法可触发RouteExceptionFactory,同时会触发Log里的Exception;
        [Route("/taskvoid")]
        public void TaskVoid(HttpListenerContext listenerContext)
        {
            throw new Exception("这是个常规任务测试异常");
        }


        //关于方法的异常捕获,此写法可触发RouteExceptionFactory,但不会触发Log里的Exception;
        [Route("/task")]
        public async Task TaskA(HttpListenerContext listenerContext)
        {
            await Task.CompletedTask;
            throw new Exception("这是个Task任务测试异常");
        }
        //这种异常不会捕获,请谨慎使用!请谨慎使用!请谨慎使用!
        //若必须使用,则请注意在内部使用try catch;
        [Route("/taskasync")]
        public async void TaskB(HttpListenerContext listenerContext)
        {
            try
            {

                await Task.CompletedTask;
                throw new Exception("这种异常不会捕获,请请注意使用!");
            }
            catch (Exception ex)
            {

                //自己处理异常或者将异常传递给路由异常工厂处理。不会触发Log里的Exception;
                this.RaiseException(listenerContext, ex);
            }
        }
  }

HttpListenerContext 扩展方法的基本使用

 internal class TestRootController : HttpControllerBase
 {


     public TestRootController()
     {
         //监听的默认路由地址以 ‘/’ 为结尾(会自动格式化)
         //base.RoutePrefix="/" ;代表监听一级路由地址,www.xxx.com与www.xxx.com/级别下的
     }

           
            
     //www.xxx.com与www.xxx.com/ 将路由到这里
     [Route("/")]
     public void Root(HttpListenerContext listenerContext)
     {
         listenerContext.SendString("这里是首页");
     }

     //www.xxx.com/testcontroll 将路由到这里,但是 www.xxx.com/testcontroll/ 不会路由到这里
     [Route("/testcontroll")]
     public void Testcontroll(HttpListenerContext httpListenerContext)
     {
         httpListenerContext.SendString("这里跟 TestController 不是一个路径");
     }

     //这里列举了关于HttpListenerContext 的响应扩展方法
     [Route("/test")]
     public void Test(HttpListenerContext listenerContext)
     {




         //发送Bytes,httpcode=200
         listenerContext.SendBytes(bytes: [], contentType: "text/plain", encoding: "utf-8");

         //发送 object 自动序列化,使用JsonHelper方法带的已经编辑好的序列化规则
         listenerContext.SendJSObject(new
         {
             success = true,
             msg = "OK",
             data = new
             {
                 time = HttpServerLite.LocalToGMT(DateTime.Now),
             }
         }, JsonHelper.JSOCamelNotNull);

         //发送 stream数据流
         using var st = new FileStream("./wwwroot/vue/index.html", FileMode.Open);
         listenerContext.SendStream(st, HttpContextType.Default_Html);

         //发送 字符串
         listenerContext.SendString("字符串");

         //使用 Sever-Sent-Event 服务
         listenerContext.UseSse(out string? lastEventID);
         listenerContext.SseSend(new HttpSSEModel() { Data = "你好", Id = lastEventID });

         //弹窗跳转
         listenerContext.AlertJump("这是弹窗信息,点击确定跳转连接~", "https://github.com/leeteke");

         //发送文件
         listenerContext.FileTransfer("./wwwroot/vue/index.html", HttpContextType.Default_Html);
         //发送文件限速
         listenerContext.FileRateLimitingTransferResult(10, 1024, "./wwwroot/vue/index.html", HttpContextType.Default_Html);
         //文件断点续传
         listenerContext.FileBreakpointResume("./wwwroot/vue/index.html", HttpContextType.Default_Html);
         //文件带限速的断点续传
         listenerContext.FileRateLimitingBreakpointResume(10, 1024, "./wwwroot/vue/index.html", HttpContextType.Default_Html);


         listenerContext.LastmodifiedCheck("./wwwroot/vue/index.html");
         listenerContext.HistroyMode("./wwwroot/vue/index.html");
         listenerContext.Close(HttpStatusCode.OK);
         listenerContext.NotModified();
         listenerContext.Abort();

        
     }
 }
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.7 152 11/28/2025
1.1.6 202 11/25/2025
1.1.5 415 11/19/2025
1.1.5-preview 413 11/19/2025
1.1.4 190 8/21/2025
1.1.3 196 7/11/2025
1.1.2 287 7/10/2025 1.1.2 is deprecated because it has critical bugs.
1.1.2-r 188 7/11/2025