LeeTeke.HttpServerLite.Hosting 1.1.7

dotnet add package LeeTeke.HttpServerLite.Hosting --version 1.1.7
                    
NuGet\Install-Package LeeTeke.HttpServerLite.Hosting -Version 1.1.7
                    
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.7" />
                    
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.7" />
                    
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.7
                    
#r "nuget: LeeTeke.HttpServerLite.Hosting, 1.1.7"
                    
#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.7
                    
#: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.7
                    
Install as a Cake Addin
#tool nuget:?package=LeeTeke.HttpServerLite.Hosting&version=1.1.7
                    
Install as a Cake Tool

LeeTeke.HttpServerLite.Hosting

· LeeTeke.HttpServerLite for Microsoft.Extensions.Hosting · 加入LeeTeke.HttpServerLite.AOT支持

Nuget

NUGET

dotnet add package LeeTeke.HttpServerLite.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)
                 //启用AOT路由
                 //已优化配置项读取,支持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();
        }
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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