LeeTeke.HttpServerLite.AOT 1.0.1

dotnet add package LeeTeke.HttpServerLite.AOT --version 1.0.1
                    
NuGet\Install-Package LeeTeke.HttpServerLite.AOT -Version 1.0.1
                    
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.AOT" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LeeTeke.HttpServerLite.AOT" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="LeeTeke.HttpServerLite.AOT" />
                    
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.AOT --version 1.0.1
                    
#r "nuget: LeeTeke.HttpServerLite.AOT, 1.0.1"
                    
#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.AOT@1.0.1
                    
#: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.AOT&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=LeeTeke.HttpServerLite.AOT&version=1.0.1
                    
Install as a Cake Tool

LeeTeke.HttpServerLite.AOT

· Native AOT for LeeTeke.HttpServerLite

依赖: LeeTeke.HttpServerLite -- version >=1.2.0 Microsoft.Extensions.DependencyInjection -- version 10.0.0

Nuget

NUGET

dotnet add package LeeTeke.HttpServerLite.AOT --version 1.0.0

基本使用方法


using LeeTeke.HttpServerLite;
using Leeteke.HttpServerLite.AOT;
using Microsoft.Extensions.DependencyInjection;

  static void Main(string[] args)
        {
           
            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>();
                    service.AddSingleton<VueController>();

                })
                 //使用 UseHttpServerLite 后会自动 将 HttpListenerBuilder 注册为 Singleton。 
                 //手动配置
                 //.UseHttpServerLite(new HttpApplicationOptions() { Port=81},HttpServerLiteConfigure)
                 //使用配置文件配置版本
                 //.UseHttpServerLite(HttpServerLiteConfigure)
                 //启用AOT路由
                .UseHttpServerLite(HttpServerLiteConfigure,HttpServerLiteAOT.Router)
                .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 !");
            });

            //启用AOT路由
            httpBuilder.UseRouterAOT(services);


            //手动使用ControllerAdd
            //【启用AOT路由后此方法不可用】
            //httpBuilder.ControllerAdd(services.GetRequiredService<TestRootController>());
            //httpBuilder.ControllerAdd(services.GetRequiredService<TestController>());

            //或者直接从IOC里面找
            //【启用AOT路由后此方法不可用】
            //httpBuilder.ControllerAddFromIoc(services);

            //提供了Vue文件的快速构建路由
            //参数输入基于RootPath位置
            //访问 www.xxx.com/vue/ 会直接运行 单页面模式 vue。
            //【启用AOT路由后此方法不可用,可创建基于VueHistoryModeRouter的类,并注册以及编辑构造函数】
            //httpBuilder.ControllerAdd(new VueHistoryModeRouter("/vue/") { UseCache_Lastmodified = true, UseGZip = true });


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

Hosting使用方法

using LeeTeke.HttpServerLite;
using LeeTeke.HttpServerLite.Hosting;
using Leeteke.HttpServerLite.AOT;
using Microsoft.Extensions.DependencyInjection;
  
            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>();
                    service.AddSingleton<VueController>();
                })
                //使用 UseHttpServerLite 后会自动 将 HttpListenerBuilder 注册为 Singleton。 
                //手动配置
                //.UseHttpServerLite(new HttpApplicationOptions() { Port=81},HttpServerLiteConfigure)
                //使用配置文件配置版本
                //会自动读取 配置文件下的 HttServerLite 配置项,该配置项为 HttpApplicationOptions的序列化,如下所示
                /*
                     "HttpServerLite": {
                        //监听地址,值类型string[]
                        "Prefixes": null,
                        //监听端口,当Prefixes存在值时,此配置项不生效
                        "Port": 80,
                        //网页根目录
                        "RootPath": "./wwwroot/",
                        //其他自定义参数,值类型string
                        "ArgStr": null
                      }
                */
                .UseHttpServerLite(HttpServerLiteConfigure,HttpServerLiteAOT.Router)
                .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 !");
            });


           //启用AOT路由
            httpBuilder.UseRouterAOT(services);


            //手动使用ControllerAdd
            //【启用AOT路由后此方法不可用】
            //httpBuilder.ControllerAdd(services.GetRequiredService<TestRootController>());
            //httpBuilder.ControllerAdd(services.GetRequiredService<TestController>());

            //或者直接从IOC里面找
            //【启用AOT路由后此方法不可用】
            //httpBuilder.ControllerAddFromIoc(services);

            //提供了Vue文件的快速构建路由
            //参数输入基于RootPath位置
            //访问 www.xxx.com/vue/ 会直接运行 单页面模式 vue。
            //【启用AOT路由后此方法不可用,可创建基于VueHistoryModeRouter的类,并注册以及编辑构造函数】
            //httpBuilder.ControllerAdd(new VueHistoryModeRouter("/vue/") { UseCache_Lastmodified = true, UseGZip = true });

            //这里不用写Run了,服务会自动启动
            //httpBuilder.Run();
        }
There are no supported framework assets in this 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.0.1 209 11/25/2025
1.0.0 409 11/19/2025
1.0.0-preview2 410 11/19/2025
1.0.0-preview 407 11/19/2025