There are a lot of way to achieve it. Here is how I do in my projects.
I normally have project name different from application name which could have spaces or much longer. So, I Keep the project name and version number in appsettings.json
file.
appsettings.json
{
"AppSettings": {
"Application": {
"Name": "ASP.NET Core Active Directory Starter Kit",
"Version": "2017.07.1"
}
}
}
Startup.cs
Load setting from appsettings.json
file into AppSettings
POCO. It then automatically register in DI container as IOptions<AppSettings>
.
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}
AppSettings.cs
Note: I have some other settings so that I keep them all together inside AppSettings POCO.
public class AppSettings
{
public Application Application { get; set; }
}
public class Application
{
public string Name { get; set; }
public string Version { get; set; }
}
Usage (_layout.cshtml)
Inject IOptions<AppSettings>
to View. You can also inject it to controller if you would like.
@inject IOptions<AppSettings> AppSettings
<footer class="main-footer">
@AppSettings.Value.Application.Version
@AppSettings.Value.Application.Name</strong>
</footer>