0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Raspberry Pi で .Net の Web アプリケーションを作成

Last updated at Posted at 2019-12-28

次のバージョンで確認しました。

$ 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"
    }
  }
}
  1. 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 にアクセス
image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?