Skip to content

Commit

Permalink
Add new bind sample /8 (#30882)
Browse files Browse the repository at this point in the history
* Add new bind sample /8

* Add new bind sample /8

* Add new bind sample /8

* Add new bind sample /8

* Add new bind sample /8

* Add new bind sample /8

* Add new bind sample /8

* Add new bind sample /8

* Add new bind sample /8

* Add new bind sample /8

* Add new bind sample /8
  • Loading branch information
Rick-Anderson committed Nov 1, 2023
1 parent cc775be commit 3aeee53
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace ConfigSample.Options
namespace ConfigSample.Options;

// <snippet>
public class ArrayExample
{
#region snippet
public class ArrayExample
{
public string[]? Entries { get; set; }
}
#endregion
public string[]? Entries { get; set; }
}
// </snippet>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ConfigSample.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.ConfigSample.Options;

namespace Microsoft.Extensions.DependencyInjection
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace ConfigSample.Options;

public abstract class SomethingWithAName
{
public abstract string? Name { get; set; }
}

public class NameTitleOptions(int age) : SomethingWithAName
{
public const string NameTitle = "NameTitle";

public override string? Name { get; set; }
public string Title { get; set; } = string.Empty;

public int Age { get; set; } = age;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace ConfigSample.Options
namespace ConfigSample.Options;

// <snippet>
public class PositionOptions
{
#region snippet
public class PositionOptions
{
public const string Position = "Position";
public const string Position = "Position";

public string Title { get; set; } = String.Empty;
public string Name { get; set; } = String.Empty;
}
#endregion
public string Title { get; set; } = String.Empty;
public string Name { get; set; } = String.Empty;
}
// </snippet>
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
using ConfigSample.Options;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;

namespace ConfigSample
{
namespace ConfigSample;

// <snippet>
public class ArrayModel : PageModel
public class ArrayModel : PageModel
{
private readonly IConfiguration Config;
public ArrayExample? _array { get; private set; }

public ArrayModel(IConfiguration config)
{
private readonly IConfiguration Config;
public ArrayExample? _array { get; private set; }
Config = config;
}

public ArrayModel(IConfiguration config)
public ContentResult OnGet()
{
_array = Config.GetSection("array").Get<ArrayExample>();
if (_array == null || _array.Entries != null)
{
Config = config;
throw new ArgumentNullException(nameof(_array));
}
string s = String.Empty;

public ContentResult OnGet()
for (int j = 0; j < _array.Entries!.Length; j++)
{
_array = Config.GetSection("array").Get<ArrayExample>();
if (_array == null)
{
throw new ArgumentNullException(nameof(_array));
}
string s = String.Empty;

for (int j = 0; j < _array.Entries.Length; j++)
{
s += $"Index: {j} Value: {_array.Entries[j]} \n";
}

return Content(s);
s += $"Index: {j} Value: {_array.Entries[j]} \n";
}

return Content(s);
}
// </snippet>
}
// </snippet>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;
using System.Linq;

namespace ConfigSample.Pages
{
#region snippet
// <snippet>
public class Index2Model : PageModel
{
private IConfigurationRoot ConfigRoot;
Expand All @@ -26,5 +26,5 @@ public ContentResult OnGet()
return Content(str);
}
}
#endregion
// </snippet>
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
using ConfigSample.Options;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;

namespace ConfigSample.Pages
{
namespace ConfigSample.Pages;

// <snippet>
public class Test21Model : PageModel
public class Test21Model : PageModel
{
private readonly IConfiguration Configuration;
public PositionOptions? positionOptions { get; private set; }

public Test21Model(IConfiguration configuration)
{
private readonly IConfiguration Configuration;
public PositionOptions? positionOptions { get; private set; }
Configuration = configuration;
}

public Test21Model(IConfiguration configuration)
public ContentResult OnGet()
{
positionOptions = Configuration.GetSection(PositionOptions.Position)
.Get<PositionOptions>();

if (positionOptions == null)
{
Configuration = configuration;
throw new ArgumentNullException(nameof(positionOptions));
}

public ContentResult OnGet()
{
positionOptions = Configuration.GetSection(PositionOptions.Position)
.Get<PositionOptions>();

return Content($"Title: {positionOptions.Title} \n" +
$"Name: {positionOptions.Name}");
}
return Content($"Title: {positionOptions.Title} \n" +
$"Name: {positionOptions.Name}");
}
// </snippet>
}
// </snippet>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@page
@model ConfigSample.Pages.Test33Model
@{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using ConfigSample.Options;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace ConfigSample.Pages;

// <snippet>
public class Test33Model : PageModel
{
private readonly IConfiguration Configuration;

public Test33Model(IConfiguration configuration)
{
Configuration = configuration;
}

public ContentResult OnGet()
{
var nameTitleOptions = new NameTitleOptions(22);
Configuration.GetSection(NameTitleOptions.NameTitle).Bind(nameTitleOptions);

return Content($"Title: {nameTitleOptions.Title} \n" +
$"Name: {nameTitleOptions.Name} \n" +
$"Age: {nameTitleOptions.Age}"
);
}
}
// </snippet>
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;

// Key/value set in appsettings.Development.json

namespace ConfigSample
{
#region snippet
// <snippet>
public class TestNumModel : PageModel
{
private readonly IConfiguration Configuration;
Expand All @@ -22,5 +22,5 @@ public ContentResult OnGet()
return Content($"{number}");
}
}
#endregion
}
// </snippet>
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;

// Test with ProgramJSONsection.cs

namespace ConfigSample
{
#region snippet
// <snippet>
public class TestSectionModel : PageModel
{
private readonly IConfiguration Config;
Expand All @@ -23,5 +23,5 @@ public ContentResult OnGet()
$"section1:key1: '{Config["key1"]}'");
}
}
#endregion
}
// </snippet>
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;

// Test with ProgramJSONsection.cs

namespace ConfigSample
{
#region snippet
// <snippet>
public class TestSection2Model : PageModel
{
private readonly IConfiguration Config;
Expand All @@ -23,5 +23,5 @@ public ContentResult OnGet()
$"section2:subsection0:key1:'{Config["key1"]}'");
}
}
#endregion
}
// </snippet>
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;

// Test with ProgramJSONsection.cs

namespace ConfigSample
{
#region snippet
// <snippet>
public class TestSection4Model : PageModel
{
private readonly IConfiguration Config;
Expand Down Expand Up @@ -37,5 +37,5 @@ public ContentResult OnGet()
return Content(s);
}
}
#endregion
}
// </snippet>
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;

namespace ConfigSample.Pages.JSON
namespace ConfigSample.Pages.JSON;

// <snippet>
public class IndexModel : PageModel
{
#region snippet
public class IndexModel : PageModel
{
private readonly IConfiguration Configuration;
private readonly IConfiguration Configuration;

public IndexModel(IConfiguration configuration)
{
Configuration = configuration;
}
public IndexModel(IConfiguration configuration)
{
Configuration = configuration;
}

public ContentResult OnGet()
{
var key00 = "section:section0:key:key0";
var key01 = "section:section0:key:key1";
var key10 = "section:section1:key:key0";
var key11 = "section:section1:key:key1";
public ContentResult OnGet()
{
var key00 = "section:section0:key:key0";
var key01 = "section:section0:key:key1";
var key10 = "section:section1:key:key0";
var key11 = "section:section1:key:key1";

var val00 = Configuration[key00];
var val01 = Configuration[key01];
var val10 = Configuration[key10];
var val11 = Configuration[key11];
var val00 = Configuration[key00];
var val01 = Configuration[key01];
var val10 = Configuration[key10];
var val11 = Configuration[key11];

return Content($"{key00} value: {val00} \n" +
$"{key01} value: {val01} \n" +
$"{key10} value: {val10} \n" +
$"{key10} value: {val11} \n"
);
}
return Content($"{key00} value: {val00} \n" +
$"{key01} value: {val01} \n" +
$"{key10} value: {val10} \n" +
$"{key10} value: {val11} \n"
);
}
#endregion
}
// </snippet>
Loading

0 comments on commit 3aeee53

Please sign in to comment.