在 ASP.NET Core 中,使用后端和 Web 服务器是指使用 .NET Core 运行时和 HTTP 服务器托管应用程序。常见的 Web 服务器包括 Kestrel、IIS 和 Apache。下面是一个使用 Kestrel 作为 Web 服务器的示例:
1.在项目文件中,添加依赖项 Microsoft.AspNetCore.Server.Kestrel:
2.在 Program.cs 文件中,将 Web 主机配置为 Kestrel:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseStartup
3.在 Startup.cs 文件中,将服务配置为使用 Kestrel:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // ... app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseMvc(); // ... }
使用上述示例代码将应用程序配置为使用 Kestrel 作为 Web 服务器。