目录
如何添加Startup.cs
文件
使用 ASP.NET Core 6.0
项目,您将找不到Startup.cs
文件。默认情况下,此文件将被删除,并且是将注册依赖项和中间件放到了Program.cs
中。但是,如果您是 Startup.cs
的粉丝或将项目升级到 ASP.NET Core 6.0
,你可能仍想要使用 Startup.cs
文件。因此,在这篇文章中,让我们将了解如何在 ASP.NET Core 6
项目中添加 Startup.cs
使用 ASP.NET Core 6.0
项目时,您的Program.cs
文件可能会这样。
Program.cs
是 ASP.NET Core Web 6应用模板的文件
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
正如您在此处看到的,服务和中间件注册现在是此文件的一部分。而 Statup.cs
有 2 个方法 ConfigureServices()
和 Configure()
,我们在方法 ConfigureServices
中注册依赖关系/服务,在 Configure
中注册中间件。
现在使用 Program.cs
,您需要在第 4 行之后注册您的服务/依赖项builder.Services.AddRazorPages();
,并在第 6 行之后注册中间件var app = builder.Build();
。请记住,在管道中注册中间件时,顺序很重要。
若要将 Startup.cs
添加到 ASP.NET Core 6.0
项目中 ,请添加一个名为Startup.cs
的新文件 并添加以下代码。
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
public void Configure(WebApplication app, IWebHostEnvironment env)
{
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
}
}
构造函数和这两个方法中的代码是非常熟悉。从文件Program.cs
中删除依赖项注册的代码并将其放入ConfigureService()
方法中。同样,中间件注册码也从文件中删除并放入Configure()
方法中。
接下来,我们需要介绍 Program.cs
如何调用 Startup.cs
。因此,请将文件代码更新为以下内容。
var builder = WebApplication.CreateBuilder(args);
var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);
var app = builder.Build();
startup.Configure(app, builder.Environment);
Startup.cs 类所需的所有对象都存在于 builder 对象中,因此我们可以将所需的对象传递给ConfigureService()
和Configure()
方法。就是这样。您的项目应该正常运行
我们还能做什么?
这是添加Startup.cs
的自定义方式,因此实际上我们不必遵循任何与该类相关的内置规则。我从不喜欢ConfigureService()
和Configure()
这两个名称,因为它们并没有确切定义它们的用途。以前,无法重命名这些名称,但现在我可以给出一个有意义的名称。类似地,ConfigureService
变成RegisterServices
,Configure
变成SetupMiddleware
。
var startup = new Startup(builder.Configuration);
startup.RegisterServices(builder.Services);
var app = builder.Build();
startup.SetupMiddlewares(app, builder.Environment);
在 ASP.NET Core 6 如何添加 Startup.cs | .NET 开发者指北 (luoyunchong.github.io)
在 ASP.NET Core 6 中添加Startup.cs的整洁方式 | .NET 开发者指北 (luoyunchong.github.io)
我的项目做法参考
.NET 8.0
Program.cs
using Server.Start;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddRazorPages();
var app = builder.Build();
startup.Configure(app, builder.Environment);
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
手动添加Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Server.IService;
using Server.Service;
using Server.Configuration;
using Server.IConfiguration;
namespace Server.Start
{
public class Startup
{
public Startup(Microsoft.Extensions.Configuration.IConfiguration configuration)
{
Configuration = configuration;
}
public Microsoft.Extensions.Configuration.IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddSingleton<IUtils, Utils>();
services.AddTransient<IConfiguration.IConfiguration, Configuration.Configuration>();
services.AddTransient<IEFContext.IEFContext, EFContext.EFContext>();
services.AddTransient<ILoginService, LoginService>();
services.AddTransient<IMenuService, MenuService>();
services.AddTransient<IUserService, UserService>();
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
此添加Startup 是为了ILoginService 和LoginService等进行注册依赖关系。