--- lang: ja-jp breaks: true --- # ASP.NET Core Web API 最小限のプログラム 2021-09-07 ## 空のプロジェクトからの変更 ![](https://i.imgur.com/oldMGmI.png) ## Startup.cs ```csharp= public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddControllers(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } ``` ## Controllers/WeatherForecastController.cs ```csharp= [Route("[controller]")] public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; [HttpGet] public IEnumerable<WeatherForecast> Get() { var rng = new Random(); return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), Summary = Summaries[rng.Next(Summaries.Length)] }) .ToArray(); } } ``` :::info `[Route("[controller]")]` を忘れると、コントローラとしてルーティングされないので注意。 ::: ## Data/WeatherForecast.cs ```csharp= public class WeatherForecast { public DateTime Date { get; set; } public int TemperatureC { get; set; } public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); public string Summary { get; set; } } ``` ## 実行 ![](https://i.imgur.com/4Hf9Cmy.png) ###### tags: `ASP.NET Core` ` Web API` `最小限のプログラム`