Startup.cs 作用就是,对项目中用到的 静态文件,管道,服务,日志,路由,数据库连接,过滤器的注册 等 所有的有关程序的启动运行中用到的
没有startup.cs可以手动新建一个Startup.cs类(具体代码可视自身情况修改)
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(); }); }}
上面写完后可以就到了第二步
program.cs内部更新逻辑
Startup.cs 类所需的所有对象都存在于 builder 对象中,因此我们可以将所需的对象传递给和方法。
var builder = WebApplication.CreateBuilder(args);var startup = new Startup(builder.Configuration);startup.ConfigureServices(builder.Services);var app = builder.Build();startup.Configure(app, builder.Environment);