Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save domingoladron/fec7df14c119bb7411056f3f64e2546a to your computer and use it in GitHub Desktop.
Save domingoladron/fec7df14c119bb7411056f3f64e2546a to your computer and use it in GitHub Desktop.
aspnetcore.webapiconventions.controller.cs
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