Skip to content

Commit 69cda83

Browse files
committed
Add StronglyTypedValidation
1 parent ed40c54 commit 69cda83

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

StronglyTypedValidation/Program.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using Microsoft.Extensions.Options;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
// Register the IOptions object
7+
// builder.Services.Configure<SlackApiSettings>(
8+
// builder.Configuration.GetSection("SlackApi"));
9+
10+
builder.Services.AddOptions<SlackApiSettings>()
11+
.BindConfiguration("SlackApi")
12+
.ValidateDataAnnotations() // <- Enable validation
13+
.ValidateOnStart(); // <- Validate on app start
14+
15+
// Explicitly register the settings object by delegating to the IOptions object
16+
builder.Services.AddSingleton(resolver =>
17+
resolver.GetRequiredService<IOptions<SlackApiSettings>>().Value);
18+
19+
var app = builder.Build();
20+
21+
// app.MapGet("/", (IOptions<SlackApiSettings> options) => options.Value);
22+
app.MapGet("/", (SlackApiSettings options) => options);
23+
24+
app.Run();
25+
26+
public class SlackApiSettings
27+
{
28+
[Required, Url]
29+
public string WebhookUrl { get; set; }
30+
[Required]
31+
public string DisplayName { get; set; }
32+
public bool ShouldNotify { get; set; }
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:27028",
7+
"sslPort": 44379
8+
}
9+
},
10+
"profiles": {
11+
"StronglyTypedValidation": {
12+
"commandName": "Project",
13+
"dotnetRunMessages": true,
14+
"launchBrowser": true,
15+
"applicationUrl": "https://localhost:7282;http://localhost:5101",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"IIS Express": {
21+
"commandName": "IISExpress",
22+
"launchBrowser": true,
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
}
26+
}
27+
}
28+
}
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>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StronglyTypedValidation", "StronglyTypedValidation.csproj", "{7F73E113-C770-48B5-A006-4FD7D5148729}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(SolutionProperties) = preSolution
14+
HideSolutionNode = FALSE
15+
EndGlobalSection
16+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17+
{7F73E113-C770-48B5-A006-4FD7D5148729}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{7F73E113-C770-48B5-A006-4FD7D5148729}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{7F73E113-C770-48B5-A006-4FD7D5148729}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{7F73E113-C770-48B5-A006-4FD7D5148729}.Release|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*",
9+
"SlackApi": {
10+
"WebhookUrl": "http://example.com/test/url",
11+
"DisplayName": null,
12+
"ShouldNotify": true
13+
}
14+
}

0 commit comments

Comments
 (0)