Skip to content
IGeekFan 开发者文档IGeekFan 开发者文档
CMS
.NET指南
FreeKit
Docker
关于
博客
github icon
    • .NET Core 学习示例文档
      • 创建简单Hello World
        • .NET Core 简单测试项目
          • FreeSql+ASP.NET Core
            • RESTful+FreeSql+AutoMapper
              • IdentityServer4
                • 七牛云对象存储
                  • ImCore 即时通讯
                    • Nacos 配置中心
                      • Serilog指南
                        • 在 ASP.NET Core 6 如何添加 Startup.cs
                          • 在 ASP.NET Core 6 中添加Startup.cs的整洁方式
                            • 源码

                          在 ASP.NET Core 6 中添加Startup.cs的整洁方式

                          author iconTalking Dotnetcalendar icon2022年6月13日category icon
                          • ASP.NET Core
                          tag icon
                          • ASP.NET Core
                          • Startup
                          timer icon大约 2 分钟word icon约 604 字

                          此页内容
                          • 源码
                          • https://www.talkingdotnet.com/clean-way-to-add-startup-class-in-asp-net-core-6-project/open in new window
                          • 作者: Talking Dotnet

                          如果您关注ASP.NET Core 6,那么您可能发现,对于ASP.NET Core 6项目,没有Startup.cs文件。我发布了关于如何在ASP.NETCore 6项目中添加Startup.csopen in new window,这种方法以传统的方式将类引入到项目中,这是我们今天使用的ASP.NET Core 5及其早期版本。但是,如果我们能够改进,让它变得更好呢。好的,在这篇文章中,让我们看一看在ASP.NET Core6中添加Startup类的整洁方法

                          为了以整洁的方式实现,我们将利用扩展方法open in new window。扩展方法允许您向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。ASP.NET Core 6.0使用和来引导ASP.NET Core应用程序。因此,我们将为这二个(WebApplicationBuilder、WebApplication)类创建扩展方法

                          记住,这2个是ASP.NET Core 6.0新引入的。阅读Comparing WebApplicationBuilder to the Generic Hostopen in new window,并Exploring the code behind WebApplicationBuilderopen in new window以了解有关此新引导模型的更多信息。

                          该类用于注册/配置服务或依赖项。由于扩展方法是静态方法,所以请使用名为的静态方法创建一个静态类。现在,将依赖项注册代码从移动到这里。如下

                          public static class RegisterStartupServices
                          {
                              public static WebApplicationBuilder RegisterServices(this WebApplicationBuilder builder)
                              {
                                  builder.Services.AddControllers();
                                  // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
                                  builder.Services.AddEndpointsApiExplorer();
                                  builder.Services.AddSwaggerGen();
                                  return builder;
                              }
                          }
                          
                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          9
                          10
                          11

                          现在,让我们创建另一个静态类RegisterStartupMiddlewares,它扩展了用于中间件注册的类WebApplication。在这个类中,从中移动中间件注册码并将其引入方法。Program.cs中的SetupMiddleware

                          public static class RegisterStartupMiddlewares
                          {
                              public static WebApplication SetupMiddleware(this WebApplication app)
                              {
                                  // Configure the HTTP request pipeline.
                                  if (app.Environment.IsDevelopment())
                                  {
                                      app.UseSwagger();
                                      app.UseSwaggerUI();
                                  }
                          
                                  app.UseHttpsRedirection();
                          
                                  app.UseAuthorization();
                          
                                  app.MapControllers();
                          
                                  return app;
                              }
                          }
                          
                          1
                          2
                          3
                          4
                          5
                          6
                          7
                          8
                          9
                          10
                          11
                          12
                          13
                          14
                          15
                          16
                          17
                          18
                          19
                          20

                          完成后,让我们返回到Program.cs中更新代码并使用这些扩展方法。如下

                          var builder = WebApplication.CreateBuilder(args)
                                       .RegisterServices();
                          
                          var app = builder.Build()
                                  .SetupMiddleware();
                          
                          app.Run();
                          
                          1
                          2
                          3
                          4
                          5
                          6
                          7

                          或者,我们可以让他更加整洁,并将其排成一行,如下

                          WebApplication.CreateBuilder(args)
                              .RegisterServices()
                              .Build()
                              .SetupMiddleware()
                              .Run();
                          
                          1
                          2
                          3
                          4
                          5

                          现在,这看起来更加清晰、可读和易于管理。就是这么简单。

                          # 源码

                          • dotnetcore-examples/Program.cs at master · luoyunchong/dotnetcore-examples (github.com)open in new window
                          edit icon在 GitHub 上编辑此页open in new window
                          上次编辑于: 2022/6/13 15:26:37
                          贡献者: igeekfan
                          上一页
                          在 ASP.NET Core 6 如何添加 Startup.cs
                          MIT Licensed | Copyright © 2021-present luoyunchong
                          苏ICP备16046457号-1

                          该应用可以安装在你的 PC 或移动设备上。这将使该 Web 应用程序外观和行为与其他应用程序相同。它将在出现在应用程序列表中,并可以固定到主屏幕,开始菜单或任务栏。此 Web 应用程序还将能够与其他应用程序和你的操作系统安全地进行交互。

                          详情