Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ProjectConfigurationDemo\ProjectConfigurationDemo.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Microsoft.Extensions.Configuration;
using ProjectConfigurationDemo.Models;

namespace ConfigurationBasics.Tests;

public class ReadingConfigurationTests
{
private static IConfiguration BuildConfiguration() =>
new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["Logging:LogLevel:Default"] = "Information",
["Logging:LogLevel:Microsoft"] = "Warning",
["ConnectionStrings:sqlConnection"] = "Server=.;Database=CodeMazeCommerce;Trusted_Connection=True"
})
.Build();

[Fact]
public void GivenAConfiguration_WhenReadWithTheIndexer_ThenItReturnsTheValueAtThatKeyPath()
{
var configuration = BuildConfiguration();

var logLevel = configuration["Logging:LogLevel:Default"];

Assert.Equal("Information", logLevel);
}

[Fact]
public void GivenAConfiguration_WhenReadWithGetValue_ThenItConvertsToTheRequestedType()
{
var configuration = BuildConfiguration();

var logLevel = configuration.GetValue<string>("Logging:LogLevel:Default");

Assert.Equal("Information", logLevel);
}

[Fact]
public void GivenAConfiguration_WhenGetConnectionStringIsCalled_ThenItReadsTheConnectionStringsSection()
{
var configuration = BuildConfiguration();

var connectionString = configuration.GetConnectionString("sqlConnection");

Assert.Equal("Server=.;Database=CodeMazeCommerce;Trusted_Connection=True", connectionString);
}

[Fact]
public void GivenAConfiguration_WhenBindIsCalled_ThenItPopulatesTheStronglyTypedObject()
{
var configuration = BuildConfiguration();
var logLevelConfiguration = new LoggingLevelConfiguration();

configuration.Bind("Logging:LogLevel", logLevelConfiguration);

Assert.Equal("Information", logLevelConfiguration.Default);
}

[Fact]
public void GivenAConfiguration_WhenAKeyIsMissing_ThenTheIndexerReturnsNullRatherThanThrowing()
{
var configuration = BuildConfiguration();

var missing = configuration["Logging:LogLevel:NotThere"];

Assert.Null(missing);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectConfigurationDemo", "ProjectConfigurationDemo\ProjectConfigurationDemo.csproj", "{D8638F89-19EC-4916-83C6-27EA52E28F84}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfigurationBasics.Tests", "ConfigurationBasics.Tests\ConfigurationBasics.Tests.csproj", "{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D8638F89-19EC-4916-83C6-27EA52E28F84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8638F89-19EC-4916-83C6-27EA52E28F84}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8638F89-19EC-4916-83C6-27EA52E28F84}.Debug|x64.ActiveCfg = Debug|Any CPU
{D8638F89-19EC-4916-83C6-27EA52E28F84}.Debug|x64.Build.0 = Debug|Any CPU
{D8638F89-19EC-4916-83C6-27EA52E28F84}.Debug|x86.ActiveCfg = Debug|Any CPU
{D8638F89-19EC-4916-83C6-27EA52E28F84}.Debug|x86.Build.0 = Debug|Any CPU
{D8638F89-19EC-4916-83C6-27EA52E28F84}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8638F89-19EC-4916-83C6-27EA52E28F84}.Release|Any CPU.Build.0 = Release|Any CPU
{D8638F89-19EC-4916-83C6-27EA52E28F84}.Release|x64.ActiveCfg = Release|Any CPU
{D8638F89-19EC-4916-83C6-27EA52E28F84}.Release|x64.Build.0 = Release|Any CPU
{D8638F89-19EC-4916-83C6-27EA52E28F84}.Release|x86.ActiveCfg = Release|Any CPU
{D8638F89-19EC-4916-83C6-27EA52E28F84}.Release|x86.Build.0 = Release|Any CPU
{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}.Debug|x64.ActiveCfg = Debug|Any CPU
{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}.Debug|x64.Build.0 = Debug|Any CPU
{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}.Debug|x86.ActiveCfg = Debug|Any CPU
{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}.Debug|x86.Build.0 = Debug|Any CPU
{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}.Release|Any CPU.Build.0 = Release|Any CPU
{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}.Release|x64.ActiveCfg = Release|Any CPU
{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}.Release|x64.Build.0 = Release|Any CPU
{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}.Release|x86.ActiveCfg = Release|Any CPU
{9F1EF799-C7E1-485D-BD6F-DDBCC3091C04}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using ProjectConfigurationDemo.Models;

namespace ProjectConfigurationDemo.Controllers;

public class HomeController(ILogger<HomeController> logger, IConfiguration configuration) : Controller
{
private readonly ILogger<HomeController> _logger = logger;
private readonly IConfiguration _configuration = configuration;

public IActionResult Index()
{
var logLevelConfiguration = new LoggingLevelConfiguration();

_configuration.Bind("Logging:LogLevel", logLevelConfiguration);

var homeModel = new HomeModel
{
DefaultLogLevel = logLevelConfiguration.Default
};

return View(homeModel);
}

public IActionResult Privacy() => View();

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error() =>
View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ProjectConfigurationDemo.Models;

public class ErrorViewModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ProjectConfigurationDemo.Models;

public class HomeModel
{
public string? DefaultLogLevel { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ProjectConfigurationDemo.Models;

public class LoggingLevelConfiguration
{
public string? Default { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

var app = builder.Build();

app.MapStaticAssets();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}")
.WithStaticAssets();

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@model HomeModel
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<span>Our default logging level is </span><strong>@Model.DefaultLogLevel</strong>
<p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@{
ViewData["Title"] = "Privacy Policy";
}
<h1>@ViewData["Title"]</h1>

<p>Use this page to detail your site's privacy policy.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@model ErrorViewModel
@{
ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
<p>
<strong>Request ID:</strong> <code>@Model.RequestId</code>
</p>
}

<h3>Development Mode</h3>
<p>
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
</p>
<p>
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
It can result in displaying sensitive information from exceptions to end users.
For local debugging, enable the <strong>Development</strong> environment by setting the
<strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> and restarting the app.
</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - ProjectConfigurationDemo</title>
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
</head>
<body>
<header>
<nav>
<a asp-area="" asp-controller="Home" asp-action="Index">ProjectConfigurationDemo</a>
<a asp-area="" asp-controller="Home" asp-action="Index">Home</a>
<a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</nav>
</header>
<div class="container">
<main role="main">
@RenderBody()
</main>
</div>

<footer>
&copy; @DateTime.Now.Year - ProjectConfigurationDemo -
<a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</footer>
<script src="~/js/site.js" asp-append-version="true"></script>
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@using ProjectConfigurationDemo
@using ProjectConfigurationDemo.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "_Layout";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ConnectionStrings": {
"sqlConnection": "Server=ProductionServerName;Database=CodeMazeCommerce;Trusted_Connection=True;TrustServerCertificate=True"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"sqlConnection": "Server=.;Database=CodeMazeCommerce;Trusted_Connection=True;TrustServerCertificate=True"
},
"OtherLoggingProvider": {
"LogLevel": {
"Default": "Debug"
}
},
"AllowedHosts": "*"
}
Loading
Loading