2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The role of Startup.cs is to register static files, pipelines, services, logs, routes, database connections, filters, etc. used in the project.
If there is no startup.cs, you can manually create a new Startup.cs class (The specific code can be modified according to your own situation)
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
-
- public IConfiguration Configuration { get; }
-
- //在依赖注入容器中注册服务
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddSingleton<IDbConfig.IDbConfig, DbConfig.DbConfig>();
- services.AddTransient<IBaseService, BaseService>();
-
- services.AddControllers();
- services.AddSwaggerGen(c =>
- {
- c.SwaggerDoc("v1", new OpenApiInfo { Title = "这里填写项目的名称", Version = "v1" });
-
- });
- }
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- if(env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- app.UseSwagger();
- app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "这里填写项目的名称 v1"));
- }
- app.UseRouting();
- app.UseAuthorization();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllers();
- });
- }
- }
After writing the above, you can go to the second step
Program.cs internal update logic
All the objects required by the Startup.cs class are present in the builder object, so we can pass the required objects to the and methods.
- var builder = WebApplication.CreateBuilder(args);
-
- var startup = new Startup(builder.Configuration);
- startup.ConfigureServices(builder.Services);
-
- var app = builder.Build();
- startup.Configure(app, builder.Environment);