---
# System prepended metadata

title: mod13
tags: [Core]

---

```csharp
// console app
//NRT: nullable reference types

string s = "aaa";
Console.WriteLine(s.ToUpper());

string s2 = null;
Console.WriteLine(s2.ToUpper());

//string? s2 = null;
//Console.WriteLine(s2?.ToUpper());

//========================
// .csproject
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>disable</Nullable>
  </PropertyGroup>
//======================
// WebsiteProfile.cs
public class WebsiteProfile
{
    public string Email { get; set; } = "default@uuu.com.tw";
    public string ThemeColor { get; set; } = "yellow";
}

// HomeController.cs
public class HomeController : Controller
{
    readonly IOptions<WebsiteProfile> _options;
    public HomeController(IOptions<WebsiteProfile> options)
    {
        _options = options;
    }

    public IActionResult Index()
    {
        return View(_options.Value);
    }
}

// home/index.cshtml
@model Mod12.Models.WebsiteProfile
@{
    ViewData["Title"] = "Home";
}

<h1>Home</h1>

<ul style="background-color:@Model.ThemeColor;">
    <li>@Model.Email</li>
    <li>@Model.ThemeColor</li>
</ul>
//===============================
// program.cs
builder.Services.Configure<WebsiteProfile>(o =>
{
    o.Email = "config@uuu.com.tw";
    o.ThemeColor = "lightblue";
});
//===============================
//appsettings.json
{
  "email": "settings@uuu.com.tw",
  "themeColor": "lightgreen",
    
// program.cs
builder.Services.AddControllersWithViews();
Services.Configure<WebsiteProfile>(builder.Configuration);
//===================
// homecontroller.cs
public class HomeController : Controller
{
    readonly IOptionsSnapshot<WebsiteProfile> _options;

    public HomeController(IOptionsSnapshot<WebsiteProfile> options)
    {
        _options = options;
    }

    public IActionResult Index()
    {
        return View(_options.Value);
    }
}
//==========================
//profile.json
{
  "email": "json@uuu.com.tw",
  "themeColor": "pink"
}

//program.cs
builder.Configuration.AddJsonFile("profile.json", true, true);

//=========================
// program.cs
builder.Configuration.AddInMemoryCollection(new Dictionary<string, string>
{
    ["Email"]="memory@uuu.com.tw",
    ["ThemeColor"] = "orange",
});
//=======================
builder.Services.Configure<WebsiteProfile>(builder.Configuration);
builder.Configuration.AddJsonFile("profile.json",true,true);
builder.Configuration.AddInMemoryCollection(new Dictionary<string, string>
{
    ["Email"]="memory@uuu.com.tw",
    ["ThemeColor"] = "orange",
});
Console.WriteLine("==============================");
foreach (var item in (builder.Configuration as IConfigurationRoot).Providers)
{
    Console.WriteLine(item);
}
Console.WriteLine("==============================");
//===============================
// profile.xml
<?xml version="1.0" encoding="utf-8" ?>
<root>
	<email>xml@uuu.com.tw</email>
	<themeColor>green</themeColor>
</root>

// program.cs
builder.Configuration.AddXmlFile("profile.xml", true, true);
//=============================== 
// profile.ini
email=ini@uuu.com.tw
themeColor=teal

// program.cs
builder.Configuration.AddIniFile("profile.ini", true, true);    
    
//=============================
// homecontroller.cs
public class HomeController : Controller
{
    readonly IOptionsSnapshot<WebsiteProfile> _options;
    readonly IConfiguration _configuration;

    public HomeController(IOptionsSnapshot<WebsiteProfile> options,
        IConfiguration configuration)
    {
        _options = options;
        _configuration = configuration;
    }
    public IActionResult GetEmail()
    {
        return Content(_configuration["email"]??"NULL");
    }
    public IActionResult Index()
    {
        return View(_options.Value);
    }
}
//=============================
// home/index.cshtml
@model Mod12.Models.WebsiteProfile
@inject IConfiguration config
@{
    ViewData["Title"] = "Home";
}

<h1>Home</h1>

<ul style="background-color:@Model.ThemeColor;">
    <li>@Model.Email</li>
    <li>@Model.ThemeColor</li>
    <li>@config["email"]</li>
</ul>
//==============================
// HomeController.cs
//public IActionResult Index()
//{
//    return View(_options.Value);
//}
public IActionResult Index()
{
    WebsiteProfile profile=new ();
    _configuration.Bind(profile);
    return View(profile);
}

//==============================
// appsettings.json
{
  "email": "xxxx@uuu.com.tw",
  "themeColor": "teal",
  "aaa": {
    "bbb": {
      "email": "zzzz@uuu.com.tw",
      "themeColor": "purple"
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

// homecontroller.cs
public IActionResult Index()
{
    WebsiteProfile profile=new ();
    _configuration.GetSection("aaa:bbb").Bind(profile);
    return View(profile);
}

//===================================
//appsettings.json
{
  "email": "xxxx@uuu.com.tw",
  "themeColor": "teal",
  "aaa": {
    "bbb": {
      "email": "zzzz@uuu.com.tw",
      "themeColor": "purple"
      "my.name": "zzzzzz"
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}
// websiteprofile.cs

    public class WebsiteProfile
    {
        public string Email { get; set; } = "default@uuu.com.tw";
        public string ThemeColor { get; set; } = "yellow";
        [ConfigurationKeyName("My.Name")]
        public string? MyName { get; set; }
    }


    
    
// home/index.cshtml
@model Mod12.Models.WebsiteProfile
@{
    ViewData["Title"] = "Home";
}

<h1>Home</h1>

<ul style="background-color:@Model.ThemeColor;">
    <li>@Model.Email.ToUpper()</li>
    <li>@Model.ThemeColor</li>
    <li>@Model.MyName?.ToUpper()</li> 
</ul>


    
```