Raspberry Pi で .Net の WebAPI を作成

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

```text
$ dotnet --version
6.0.302
```

次のページで行っているうちのページの途中までを行いました。
[Building and Securing Web APIs with ASP.NET Core 3.0](https://auth0.com/blog/how-to-build-and-secure-web-apis-with-aspnet-core-3/#Creating-a-CRUD-API)

Glossary の項目のひつとを Get するところまでです。

localhost からは次のように取り出しができます。

```text
$ curl --insecure https://localhost:5001/api/glossary/jwt | jq .
{
  "term": "JWT",
  "definition": "An open, industry standard RFC 7519 method for representing claims securely between two parties. "
}
```

他のホストからは次のように取り出しができます。

```text
$ curl --insecure https://violet.local:5001/api/glossary/jwt | jq
{
  "term": "JWT",
  "definition": "An open, industry standard RFC 7519 method for representing claims securely between two parties. "
}
```

1) プロジェクトの作成

```bash
dotnet new webapi -o Glossary
```

２）他のホストからアクセスが出来るように、次のファイルを書き換えます。

```json:appsettings.Development.json
{
 "urls": "http://*:5000;https://*:5001",
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  }
}
```

```json:appsettings.json
{
"urls": "http://*:5000;https://*:5001",
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

```

３）この時点で実行

```bash
dotnet build
dotnet bin/Debug/net6.0/Glossary.dll
```

４）curl でアクセスして確認

```bash
curl --insecure https://localhost:5001/weatherforecast
```

他の端末からアクセス

```bash
curl --insecure https://violet.local:5001/weatherforecast
```

５）改造するにあたり不要となるファイルの削除

```bash
rm WeatherForecast.cs
rm Controllers/WeatherForecastController.cs
```

6) Glossary 関連のファイルの作成

データの項目は次の３つです。
　"Access Token", "JWT", "OpenID"

```cs:GlossaryItem.cs
//GlossaryItem.cs
namespace Glossary
{
    public class GlossaryItem
    {
        public string? Term { get; set; }
        public string? Definition { get; set; }
    }
}
```

```cs:Controllers/GlossaryController.cs
//Controllers/GlossaryController.cs
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;

namespace Glossary.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class GlossaryController: ControllerBase
    {
        private static List<GlossaryItem> Glossary = new List<GlossaryItem> {
        //leave the glossary untouched
            new GlossaryItem
            {
                Term= "Access Token",
                Definition = "A credential that can be used by an application to access an API. It informs the API that the bearer of the token has been authorized to access the API and perform specific actions specified by the scope that has been granted."
            },
            new GlossaryItem
            {
                Term= "JWT",
                Definition = "An open, industry standard RFC 7519 method for representing claims securely between two parties. "
            },
            new GlossaryItem
            {
                Term= "OpenID",
                Definition = "An open standard for authentication that allows applications to verify users are who they say they are without needing to collect, store, and therefore become liable for a user’s login information."
            }
        };


        //new code
        [HttpGet]
        [Route("{term}")]
        public ActionResult<GlossaryItem> Get(string term)
        {
                Console.Error.WriteLine("*** Get *** " + term);
            var glossaryItem = Glossary.Find(item =>
                    item.Term.Equals(term, StringComparison.InvariantCultureIgnoreCase));

            if (glossaryItem == null)
            {
                return NotFound();
            } else
            {
                return Ok(glossaryItem);
            }
        }
        //end new code
    }
}
```

7）実行

```bash
dotnet build
dotnet bin/Debug/net6.0/Glossary.dll
```

8）curl でアクセスして確認

```bash
curl --insecure https://localhost:5001/api/glossary/jwt
```

他の端末からアクセス

```bash
curl --insecure https://violet.local:5001/api/glossary/jwt
```