次のバージョンで確認しました。
$ dotnet --version
6.0.302
プロジェクトの作成
mkdir Web01
cd Web01
dotnet new web
$ mkdir Web01
$ cd Web01
$ dotnet new web
The template "ASP.NET Core Empty" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on /home/uchida/tmp/Web01/Web01.csproj...
/home/uchida/tmp/Web01/Web01.csproj の復元が 424.71 ms で完了しました。
Restore succeeded.
次のようなファイルが作成されます。
$ tree
.
├── appsettings.Development.json
├── appsettings.json
├── obj
│ ├── project.assets.json
│ ├── project.nuget.cache
│ ├── Web01.csproj.nuget.dgspec.json
│ ├── Web01.csproj.nuget.g.props
│ └── Web01.csproj.nuget.g.targets
├── Program.cs
├── Properties
│ └── launchSettings.json
└── Web01.csproj
2 directories, 10 files
次の2つのファイルを修正します。
1) localhost 以外からもアクセスできるようにします。
appsettings.Development.json
{
"urls": "http://*:5000",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
- html ファイルを送るようにします。
Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.MapGet("/morning", () => "Good Morning!");
app.MapGet("/afternoon", () => "Good Afernoon!");
app.MapGet("/evening", () => "Good Evening!");
app.MapGet("/html", async context =>
{
String ssx = "<!DOCTYPE html>";
ssx += "<html lang='ja'>";
ssx += "<meta charset='UTF-8'>";
ssx += "<body>";
ssx += "<p>こんにちは<p />";
ssx += "<hr />";
ssx += "<p>Aug/06/2022<p />";
ssx += "</body>";
ssx += "</html>";
await context.Response.WriteAsync(ssx);
});
app.Run();
コンパイルと実行
$ dotnet run
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://[::]:5000
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /home/uchida/tmp/Web01/
ブラウザーで http://host:5000/html にアクセス