Skip to content

Commit c3cb891

Browse files
Add TryParse samples (dotnet#4)
1 parent 7cf401f commit c3cb891

21 files changed

+447
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Diagnostics;
2+
using BindTryParseMVC.Models;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace BindTryParseMVC.Controllers
6+
{
7+
public class HomeController : Controller
8+
{
9+
private readonly ILogger<HomeController> _logger;
10+
11+
public HomeController(ILogger<HomeController> logger)
12+
{
13+
_logger = logger;
14+
}
15+
16+
public IActionResult Index()
17+
{
18+
return View();
19+
}
20+
21+
public IActionResult Privacy()
22+
{
23+
return View();
24+
}
25+
26+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
27+
public IActionResult Error()
28+
{
29+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
30+
}
31+
}
32+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Globalization;
2+
using BindTryParseMVC.Models;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
namespace BindTryParseMVC.Controllers
6+
{
7+
public class WeatherForecastController : Controller
8+
{
9+
private static readonly string[] Summaries = new[]
10+
{
11+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
12+
};
13+
14+
// <snippet>
15+
//GET /WeatherForecast?culture=en-GB
16+
public IActionResult Index(Culture? culture)
17+
{
18+
var weatherForecasts = Enumerable
19+
.Range(1, 5).Select(index => new WeatherForecast
20+
{
21+
Date = DateTime.Now.AddDays(index),
22+
TemperatureC = Random.Shared.Next(-20, 55),
23+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
24+
})
25+
.Select(wf => new WeatherForecastViewModel
26+
{
27+
Date = wf.Date.ToString(new CultureInfo(culture?.DisplayName ?? "en-US")),
28+
TemperatureC = wf.TemperatureC,
29+
TemperatureF = wf.TemperatureF,
30+
Summary = wf.Summary
31+
});
32+
33+
return View(weatherForecasts);
34+
}
35+
// </snippet>
36+
37+
// GET /WeatherForecast/Range?range=07/12/2022-07/14/2022
38+
// <snippet_1>
39+
public IActionResult Range(DateRange? range)
40+
{
41+
var weatherForecasts = Enumerable
42+
.Range(1, 5).Select(index => new WeatherForecast
43+
{
44+
Date = DateTime.Now.AddDays(index),
45+
TemperatureC = Random.Shared.Next(-20, 55),
46+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
47+
})
48+
.Where(wf => DateOnly.FromDateTime(wf.Date) >= (range?.From ?? DateOnly.MinValue)
49+
&& DateOnly.FromDateTime(wf.Date) <= (range?.To ?? DateOnly.MaxValue))
50+
.Select(wf => new WeatherForecastViewModel
51+
{
52+
Date = wf.Date.ToString(CultureInfo.InvariantCulture),
53+
TemperatureC = wf.TemperatureC,
54+
TemperatureF = wf.TemperatureF,
55+
Summary = wf.Summary
56+
});
57+
58+
return View("Index", weatherForecasts);
59+
}
60+
// </snippet_1>
61+
}
62+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace BindTryParseMVC.Models
2+
{
3+
// <snippet>
4+
public class Culture
5+
{
6+
public string? DisplayName { get; }
7+
8+
public Culture(string displayName)
9+
{
10+
if (string.IsNullOrEmpty(displayName))
11+
throw new ArgumentNullException(nameof(displayName));
12+
13+
DisplayName = displayName;
14+
}
15+
16+
public static bool TryParse(string? value, out Culture? result)
17+
{
18+
if (value is null)
19+
{
20+
result = default;
21+
return false;
22+
}
23+
24+
result = new Culture(value);
25+
return true;
26+
}
27+
}
28+
// </snippet>
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace BindTryParseMVC.Models
2+
{
3+
// <snippet>
4+
public class DateRange
5+
{
6+
public DateOnly? From { get; }
7+
public DateOnly? To { get; }
8+
9+
public DateRange(string from, string to)
10+
{
11+
if (string.IsNullOrEmpty(from))
12+
throw new ArgumentNullException(nameof(from));
13+
if (string.IsNullOrEmpty(to))
14+
throw new ArgumentNullException(nameof(to));
15+
16+
From = DateOnly.Parse(from);
17+
To = DateOnly.Parse(to);
18+
}
19+
20+
public static bool TryParse(string? value, IFormatProvider? provider, out DateRange? result)
21+
{
22+
if (string.IsNullOrEmpty(value) || value.Split('-').Length != 2)
23+
{
24+
result = default;
25+
return false;
26+
}
27+
28+
var range = value.Split('-');
29+
result = new DateRange(range[0], range[1]);
30+
return true;
31+
}
32+
}
33+
// </snippet>
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BindTryParseMVC.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BindTryParseMVC.Models
2+
{
3+
public class WeatherForecast
4+
{
5+
public DateTime Date { get; set; }
6+
7+
public int TemperatureC { get; set; }
8+
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
10+
11+
public string? Summary { get; set; }
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BindTryParseMVC.Models
2+
{
3+
public class WeatherForecastViewModel
4+
{
5+
public string? Date { get; set; }
6+
7+
public int TemperatureC { get; set; }
8+
9+
public int TemperatureF { get; set; }
10+
11+
public string? Summary { get; set; }
12+
}
13+
}

mvc/models/BindTryParseMVC/Program.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add services to the container.
4+
builder.Services.AddControllersWithViews();
5+
6+
var app = builder.Build();
7+
8+
// Configure the HTTP request pipeline.
9+
if (!app.Environment.IsDevelopment())
10+
{
11+
app.UseExceptionHandler("/Home/Error");
12+
}
13+
14+
app.UseStaticFiles();
15+
16+
app.UseRouting();
17+
18+
app.UseAuthorization();
19+
20+
app.MapControllerRoute(
21+
name: "default",
22+
pattern: "{controller=Home}/{action=Index}/{id?}");
23+
24+
app.Run();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div class="text-center">
6+
<h1 class="display-4">Welcome</h1>
7+
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
8+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@model ErrorViewModel
2+
@{
3+
ViewData["Title"] = "Error";
4+
}
5+
6+
<h1 class="text-danger">Error.</h1>
7+
<h2 class="text-danger">An error occurred while processing your request.</h2>
8+
9+
@if (Model.ShowRequestId)
10+
{
11+
<p>
12+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
13+
</p>
14+
}
15+
16+
<h3>Development Mode</h3>
17+
<p>
18+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19+
</p>
20+
<p>
21+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22+
It can result in displaying sensitive information from exceptions to end users.
23+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
24+
and restarting the app.
25+
</p>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>@ViewData["Title"] - BindTryParseMVC</title>
7+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.css" />
8+
<link rel="stylesheet" href="~/site.css" asp-append-version="true" />
9+
<link rel="stylesheet" href="~/BindTryParseMVC.styles.css" asp-append-version="true" />
10+
</head>
11+
<body>
12+
<header>
13+
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
14+
<div class="container-fluid">
15+
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">BindTryParseMVC</a>
16+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
17+
aria-expanded="false" aria-label="Toggle navigation">
18+
<span class="navbar-toggler-icon"></span>
19+
</button>
20+
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
21+
<ul class="navbar-nav flex-grow-1">
22+
<li class="nav-item">
23+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
24+
</li>
25+
<li class="nav-item">
26+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
27+
</li>
28+
<li class="nav-item">
29+
<a class="nav-link text-dark" asp-area="" asp-controller="WeatherForecast" asp-action="Index">Weather Forecast</a>
30+
</li>
31+
</ul>
32+
</div>
33+
</div>
34+
</nav>
35+
</header>
36+
<div class="container">
37+
<main role="main" class="pb-3">
38+
@RenderBody()
39+
</main>
40+
</div>
41+
42+
<footer class="border-top footer text-muted">
43+
<div class="container">
44+
&copy; 2022 - BindTryParseMVC - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
45+
</div>
46+
</footer>
47+
48+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.js"></script>
49+
</body>
50+
</html>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2+
for details on configuring this project to bundle and minify static web assets. */
3+
4+
a.navbar-brand {
5+
white-space: normal;
6+
text-align: center;
7+
word-break: break-all;
8+
}
9+
10+
a {
11+
color: #0077cc;
12+
}
13+
14+
.btn-primary {
15+
color: #fff;
16+
background-color: #1b6ec2;
17+
border-color: #1861ac;
18+
}
19+
20+
.nav-pills .nav-link.active, .nav-pills .show > .nav-link {
21+
color: #fff;
22+
background-color: #1b6ec2;
23+
border-color: #1861ac;
24+
}
25+
26+
.border-top {
27+
border-top: 1px solid #e5e5e5;
28+
}
29+
30+
.border-bottom {
31+
border-bottom: 1px solid #e5e5e5;
32+
}
33+
34+
.box-shadow {
35+
box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
36+
}
37+
38+
button.accept-policy {
39+
font-size: 1rem;
40+
line-height: inherit;
41+
}
42+
43+
.footer {
44+
position: absolute;
45+
bottom: 0;
46+
width: 100%;
47+
white-space: nowrap;
48+
line-height: 60px;
49+
}

0 commit comments

Comments
 (0)