Skip to content

Instantly share code, notes, and snippets.

@aetos382
Last active February 28, 2024 16:15
Show Gist options
  • Save aetos382/5569b73be7ef7eb210a023deae8d7606 to your computer and use it in GitHub Desktop.
Save aetos382/5569b73be7ef7eb210a023deae8d7606 to your computer and use it in GitHub Desktop.
ASP.NET Core の Program.cs の書き方の変遷

ASP.NET Core の Program.cs の書き方の変遷

ASP.NET Core 1 ~ 5 では UseStartup<T> を使うのが一般的でしたが、使わなくても同等の内容は書けるので、今回は使いませんでした。

また、IConfguration やロギングやその他のサービスは敢えて省いています。

ASP.NET Core 5 は ASP.NET Core 3 と同じです。

ASP.NET Core 7 および 8 は ASP.NET Core 6 と同じです。

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
new WebHostBuilder()
.ConfigureServices(static services =>
{
})
.Configure(static app =>
{
app.Use(static _ => static context => context.Response.WriteAsync("Hello, ASP.NET Core 1"));
})
.UseKestrel()
.UseUrls("http://localhost:5001")
.Build()
.Run();
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
WebHost
.CreateDefaultBuilder()
.ConfigureServices(static services =>
{
})
.Configure(static app =>
{
app.Use(static _ => context => context.Response.WriteAsync("Hello, ASP.NET Core 2"));
})
.UseUrls("http://localhost:5002")
.Build()
.Run();
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
Host
.CreateDefaultBuilder()
.ConfigureWebHostDefaults(static webHostBuilder => webHostBuilder
.ConfigureServices(static services =>
{
})
.Configure(static app =>
{
app.Use(static _ => static context => context.Response.WriteAsync("Hello, ASP.NET Core 3"));
})
.UseUrls("http://localhost:5003"))
.Build()
.Run();
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
var builder = WebApplication.CreateBuilder();
// builder.Services.AddXxx();
var app = builder.Build();
app.Use(static _ => static context => context.Response.WriteAsync("Hello, ASP.NET Core 6"));
app.Run("http://localhost:5006");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment