Skip to content

Commit c88a902

Browse files
Add ASP.NET Core web app example (#246)
* Add ASP.NET Core web app example * Change the ASP.NET Core example target platform from aspnetcore3.1 to aspnetcore2.1 * Simplify the project sample Co-authored-by: Aaron Stannard <[email protected]>
1 parent 828a292 commit c88a902

16 files changed

+354
-0
lines changed

Hocon.sln

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hocon.API.Tests", "src\Hoco
3535
EndProject
3636
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hocon.Tests.Performance", "src\Hocon.Tests.Performance\Hocon.Tests.Performance.csproj", "{BD958665-7CE8-4DEF-89EA-8933393016D8}"
3737
EndProject
38+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetWebApp", "src\examples\AspNetWebApp\AspNetWebApp.csproj", "{181D6251-3B11-4F2D-92E5-631C99D9F791}"
39+
EndProject
3840
Global
3941
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4042
Debug|Any CPU = Debug|Any CPU
@@ -101,6 +103,10 @@ Global
101103
{BD958665-7CE8-4DEF-89EA-8933393016D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
102104
{BD958665-7CE8-4DEF-89EA-8933393016D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
103105
{BD958665-7CE8-4DEF-89EA-8933393016D8}.Release|Any CPU.Build.0 = Release|Any CPU
106+
{181D6251-3B11-4F2D-92E5-631C99D9F791}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
107+
{181D6251-3B11-4F2D-92E5-631C99D9F791}.Debug|Any CPU.Build.0 = Debug|Any CPU
108+
{181D6251-3B11-4F2D-92E5-631C99D9F791}.Release|Any CPU.ActiveCfg = Release|Any CPU
109+
{181D6251-3B11-4F2D-92E5-631C99D9F791}.Release|Any CPU.Build.0 = Release|Any CPU
104110
EndGlobalSection
105111
GlobalSection(SolutionProperties) = preSolution
106112
HideSolutionNode = FALSE
@@ -111,6 +117,7 @@ Global
111117
{603AC356-4D9D-490C-BE69-685B792D2FD4} = {C3695FA9-B189-4881-864A-8377EE987B74}
112118
{1437D06E-EA95-4599-A6D0-80C1EC774A5A} = {C3695FA9-B189-4881-864A-8377EE987B74}
113119
{7A497666-0799-4165-88A4-1DB37DB65A8D} = {C3695FA9-B189-4881-864A-8377EE987B74}
120+
{181D6251-3B11-4F2D-92E5-631C99D9F791} = {C3695FA9-B189-4881-864A-8377EE987B74}
114121
EndGlobalSection
115122
GlobalSection(ExtensibilityGlobals) = postSolution
116123
SolutionGuid = {557DB117-9178-4935-B327-3017C53186FE}

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -1113,3 +1113,18 @@ way to get rid of default fallback values they don't want.
11131113

11141114
Config keys are encouraged to be `hyphen-separated` rather than
11151115
`camelCase`.
1116+
1117+
# Hocon.Extensions.Configuration
1118+
`Hocon.Extensions.Configuration` is an extension of HOCON that
1119+
allows HOCON configuration files to be read and loaded into `Microsoft.Extensions.Configuration`
1120+
1121+
## Installation
1122+
To install [`Microsoft.Extensions.Configuration` via NuGet](https://www.nuget.org/packages/Hocon.Extensions.Configuration/):
1123+
1124+
```
1125+
PS> Install-Package Hocon.Extensions.Configuration
1126+
```
1127+
1128+
## Examples
1129+
An example project on how to use `Hocon.Extensions.Configuration` with ASP.NET Core
1130+
Web Application can be seen [in the examples folder](https://github.com/akkadotnet/HOCON/tree/dev/src/examples/AspNetWebApp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Content Include="appsettings.conf">
9+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
10+
</Content>
11+
<Content Include="appsettings.Development.conf">
12+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
13+
<DependentUpon>appsettings.conf</DependentUpon>
14+
</Content>
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="Hocon.Extensions.Configuration" Version="2.0.1" />
19+
<PackageReference Include="Microsoft.AspNetCore.App" />
20+
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
21+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.2" />
22+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.2" />
23+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.2" />
24+
<PackageReference Include="Microsoft.Extensions.FileProviders.Abstractions" Version="3.1.2" />
25+
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="3.1.2" />
26+
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="3.1.2" />
27+
<PackageReference Include="Microsoft.Extensions.Primitives" Version="3.1.2" />
28+
</ItemGroup>
29+
30+
<ItemGroup>
31+
<Folder Include="wwwroot\" />
32+
</ItemGroup>
33+
34+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Mvc;
7+
using AspNetWebApp.Models;
8+
using Hocon;
9+
10+
namespace AspNetWebApp.Controllers
11+
{
12+
public class HomeController : Controller
13+
{
14+
private string _hocon;
15+
16+
public HomeController()
17+
{
18+
_hocon = System.IO.File.ReadAllText("appsettings.conf");
19+
}
20+
21+
public IActionResult Index()
22+
{
23+
ViewData["HOCON"] = _hocon;
24+
return View();
25+
}
26+
27+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
28+
public IActionResult Error()
29+
{
30+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace AspNetWebApp.Models
4+
{
5+
public class ErrorViewModel
6+
{
7+
public string RequestId { get; set; }
8+
9+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
10+
}
11+
}

src/examples/AspNetWebApp/Program.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using System.Reflection;
7+
using Microsoft.AspNetCore;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.Extensions.Configuration;
10+
using Microsoft.Extensions.Logging;
11+
using Hocon.Extensions.Configuration;
12+
13+
namespace AspNetWebApp
14+
{
15+
public class Program
16+
{
17+
public static void Main(string[] args)
18+
{
19+
CreateWebHostBuilder(args).Build().Run();
20+
}
21+
22+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
23+
WebHost.CreateDefaultBuilder(args)
24+
.UseStartup<Startup>()
25+
.ConfigureAppConfiguration((hostingContext, config) =>
26+
{
27+
// We inject the HOCON configuration file using this function call,
28+
// the rest of the code are there to make sure that the final configuration
29+
// conforms to the Microsoft standard on loading a full configuration stack.
30+
var env = hostingContext.HostingEnvironment;
31+
config.AddHoconFile("appsettings.conf", optional: false, reloadOnChange: true)
32+
.AddHoconFile($"appsettings.{env.EnvironmentName}.conf", optional: true, reloadOnChange: true);
33+
34+
if (env.IsDevelopment())
35+
{
36+
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
37+
if (appAssembly != null)
38+
{
39+
config.AddUserSecrets(appAssembly, optional: true);
40+
}
41+
}
42+
43+
config.AddEnvironmentVariables();
44+
45+
if (args != null)
46+
{
47+
config.AddCommandLine(args);
48+
}
49+
});
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:51951",
7+
"sslPort": 44305
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"AspNetWebApp": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
}
25+
}
26+
}
27+
}

src/examples/AspNetWebApp/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Hocon.Extension.Configuration Web Application Sample
2+
3+
This is an example on how to use `Hocon.Extension.Configuration` nuget package with an ASP.NET Core 3.1 Web Application project.
4+
The project file was made in Visual Studio 2019.
5+
6+
The code needed to inject the HOCON configuration file into the Web Application configuration singleton instance
7+
is located inside the `Program.cs` file.
8+
9+
```C#
10+
public static IHostBuilder CreateHostBuilder(string[] args) =>
11+
Host.CreateDefaultBuilder(args)
12+
.ConfigureWebHostDefaults(webBuilder =>
13+
{
14+
webBuilder.UseStartup<Startup>();
15+
})
16+
.ConfigureAppConfiguration((hostingContext, config) =>
17+
{
18+
// We inject the HOCON configuration file using this function call,
19+
// the rest of the code are there to make sure that the final configuration
20+
// conforms to the Microsoft standard on loading a full configuration stack.
21+
var env = hostingContext.HostingEnvironment;
22+
config.AddHoconFile(
23+
"appsettings.conf",
24+
optional: false,
25+
reloadOnChange: true)
26+
.AddHoconFile(
27+
$"appsettings.{env.EnvironmentName}.conf",
28+
optional: true,
29+
reloadOnChange: true);
30+
31+
if (env.IsDevelopment())
32+
{
33+
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
34+
if (appAssembly != null)
35+
{
36+
config.AddUserSecrets(appAssembly, optional: true);
37+
}
38+
}
39+
40+
config.AddEnvironmentVariables();
41+
42+
if (args != null)
43+
{
44+
config.AddCommandLine(args);
45+
}
46+
});
47+
}
48+
```

src/examples/AspNetWebApp/Startup.cs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.HttpsPolicy;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Microsoft.Extensions.Configuration;
11+
using Microsoft.Extensions.DependencyInjection;
12+
13+
namespace AspNetWebApp
14+
{
15+
public class Startup
16+
{
17+
public Startup(IConfiguration configuration)
18+
{
19+
Configuration = configuration;
20+
}
21+
22+
public IConfiguration Configuration { get; }
23+
24+
// This method gets called by the runtime. Use this method to add services to the container.
25+
public void ConfigureServices(IServiceCollection services)
26+
{
27+
services.Configure<CookiePolicyOptions>(options =>
28+
{
29+
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
30+
options.CheckConsentNeeded = context => true;
31+
options.MinimumSameSitePolicy = SameSiteMode.None;
32+
});
33+
34+
35+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
36+
}
37+
38+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
39+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
40+
{
41+
if (env.IsDevelopment())
42+
{
43+
app.UseDeveloperExceptionPage();
44+
}
45+
else
46+
{
47+
app.UseExceptionHandler("/Home/Error");
48+
app.UseHsts();
49+
}
50+
51+
app.UseHttpsRedirection();
52+
app.UseStaticFiles();
53+
app.UseCookiePolicy();
54+
55+
app.UseMvc(routes =>
56+
{
57+
routes.MapRoute(
58+
name: "default",
59+
template: "{controller=Home}/{action=Index}/{id?}");
60+
});
61+
}
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@{
2+
ViewData["Title"] = "HOCON content";
3+
}
4+
5+
<div style="white-space:pre-wrap;">
6+
@Html.Raw(ViewData["HOCON"])
7+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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>Development environment should not be enabled in deployed applications</strong>, as it can result in sensitive information from exceptions being displayed to end users. For local debugging, development environment can be enabled by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>, and restarting the application.
22+
</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>@ViewData["Title"] - Using HOCON With ASP.NET Core Example</title>
6+
</head>
7+
<body>
8+
@RenderBody()
9+
</body>
10+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@using AspNetWebApp
2+
@using AspNetWebApp.Models
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
Layout = "_Layout";
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
Logging = {
3+
LogLevel = {
4+
Default = Information
5+
Microsoft = Warning
6+
"Microsoft.Hosting.Lifetime" = Information
7+
}
8+
}
9+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
Logging = {
3+
LogLevel = {
4+
Default = Information
5+
Microsoft = Warning
6+
"Microsoft.Hosting.Lifetime" = Information
7+
}
8+
}
9+
10+
AllowedHosts = "*"
11+
}

0 commit comments

Comments
 (0)