Clean up TryParse samples

pull/26433/head
Rick Anderson 2022-07-14 11:09:03 -10:00
parent c05b6c0bb7
commit f7ce7c88cb
6 changed files with 22 additions and 11 deletions

View File

@ -20,6 +20,8 @@ namespace BindTryParseAPI.Controllers
_logger = logger;
}
// <snippet2>
// GET /WeatherForecast?culture=en-GB
[HttpGet]
public IActionResult Get([FromQuery] Culture? culture)
{
@ -40,7 +42,10 @@ namespace BindTryParseAPI.Controllers
return Ok(weatherForecasts);
}
// </snippet2>
// <snippet>
// GET /WeatherForecast/GetByRange?range=07/12/2022-07/14/2022
[HttpGet]
[Route("GetByRange")]
public IActionResult Range([FromQuery] DateRange? range)
@ -63,5 +68,6 @@ namespace BindTryParseAPI.Controllers
return Ok(weatherForecasts);
}
// </snippet>
}
}

View File

@ -1,5 +1,6 @@
namespace BindTryParseAPI.Models
{
// <snippet>
public class Culture
{
public string? DisplayName { get; }
@ -24,4 +25,5 @@ namespace BindTryParseAPI.Models
return true;
}
}
// </snippet>
}

View File

@ -1,5 +1,6 @@
namespace BindTryParseAPI.Models
{
// <snippet>
public class DateRange
{
public DateOnly? From { get; }
@ -29,4 +30,5 @@ namespace BindTryParseAPI.Models
return true;
}
}
// </snippet>
}

View File

@ -11,11 +11,8 @@ namespace BindTryParseMVC.Controllers
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
/// <summary>
/// /WeatherForecast?culture=en-GB
/// </summary>
/// <param name="culture"></param>
/// <returns></returns>
// <snippet>
//GET /WeatherForecast?culture=en-GB
public IActionResult Index(Culture? culture)
{
var weatherForecasts = Enumerable
@ -35,12 +32,10 @@ namespace BindTryParseMVC.Controllers
return View(weatherForecasts);
}
// </snippet>
/// <summary>
/// /WeatherForecast/Range?range=07/12/2022-07/14/2022
/// </summary>
/// <param name="range"></param>
/// <returns></returns>
// GET /WeatherForecast/Range?range=07/12/2022-07/14/2022
// <snippet_1>
public IActionResult Range(DateRange? range)
{
var weatherForecasts = Enumerable
@ -50,7 +45,8 @@ namespace BindTryParseMVC.Controllers
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.Where(wf => DateOnly.FromDateTime(wf.Date) >= (range?.From ?? DateOnly.MinValue) && DateOnly.FromDateTime(wf.Date) <= (range?.To ?? DateOnly.MaxValue))
.Where(wf => DateOnly.FromDateTime(wf.Date) >= (range?.From ?? DateOnly.MinValue)
&& DateOnly.FromDateTime(wf.Date) <= (range?.To ?? DateOnly.MaxValue))
.Select(wf => new WeatherForecastViewModel
{
Date = wf.Date.ToString(CultureInfo.InvariantCulture),
@ -61,5 +57,6 @@ namespace BindTryParseMVC.Controllers
return View("Index", weatherForecasts);
}
// </snippet_1>
}
}

View File

@ -1,5 +1,6 @@
namespace BindTryParseMVC.Models
{
// <snippet>
public class Culture
{
public string? DisplayName { get; }
@ -24,4 +25,5 @@ namespace BindTryParseMVC.Models
return true;
}
}
// </snippet>
}

View File

@ -1,5 +1,6 @@
namespace BindTryParseMVC.Models
{
// <snippet>
public class DateRange
{
public DateOnly? From { get; }
@ -29,4 +30,5 @@ namespace BindTryParseMVC.Models
return true;
}
}
// </snippet>
}