Created
May 23, 2020 00:05
-
-
Save domingoladron/fec7df14c119bb7411056f3f64e2546a to your computer and use it in GitHub Desktop.
aspnetcore.webapiconventions.controller.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Logging; | |
namespace AspNetCore.Conventions.Controllers | |
{ | |
[ApiController] | |
[ApiConventionType(typeof(DefaultApiConventions))] | |
[Route("[controller]")] | |
public class WeatherForecastController : ControllerBase | |
{ | |
private static readonly string[] Summaries = new[] | |
{ | |
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | |
}; | |
private readonly ILogger<WeatherForecastController> _logger; | |
public WeatherForecastController(ILogger<WeatherForecastController> logger) | |
{ | |
_logger = logger; | |
} | |
[HttpGet] | |
public async Task<ActionResult<IEnumerable<WeatherForecast>>> GetAsync() | |
{ | |
var rng = new Random(); | |
var notFound = false; | |
var response = Enumerable.Range(1, 5).Select(index => new WeatherForecast | |
{ | |
Date = DateTime.Now.AddDays(index), | |
TemperatureC = rng.Next(-20, 55), | |
Summary = Summaries[rng.Next(Summaries.Length)] | |
}); | |
//return a NotFound Result | |
if(notFound) | |
{ | |
return new NotFoundResult(); | |
} | |
return await Task.Run(() => new OkObjectResult(response)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment