weather.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Microsoft.AspNetCore.Mvc;
  9. using System.Collections.Generic;
  10. using System.Threading.Tasks;
  11. namespace EasyTemplate.Service
  12. {
  13. [ApiController]
  14. [Route("[controller]")]
  15. public class WeatherForecastController : ControllerBase
  16. {
  17. private static readonly string[] Summaries = new[]
  18. {
  19. "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
  20. };
  21. private readonly ILogger<WeatherForecastController> _logger;
  22. public WeatherForecastController(ILogger<WeatherForecastController> logger)
  23. {
  24. _logger = logger;
  25. }
  26. [HttpGet]
  27. public IEnumerable<WeatherForecast> Get()
  28. {
  29. var rng = new Random();
  30. return Enumerable.Range(1, 5).Select(index => new WeatherForecast
  31. {
  32. Date = DateTime.Now.AddDays(index),
  33. TemperatureC = rng.Next(-20, 55),
  34. Summary = Summaries[rng.Next(Summaries.Length)]
  35. })
  36. .ToArray();
  37. }
  38. }
  39. public class WeatherForecast
  40. {
  41. public DateTime Date { get; set; }
  42. public int TemperatureC { get; set; }
  43. public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
  44. public string Summary { get; set; }
  45. }
  46. }