I would like do display in the footer of an ASP.NET Core 1.1
© 2017 MyApplication
<p>© 2017 @(ApplicationName)</p>
How do I get the application name?
I found an article on this subject but it's confusing about PlatformServices.Default.Application.ApplicationName
, because it says do not use Microsoft.Extensions.PlatformAbstractions
, but does not say what to use instead for the application name...
You could try:
@using System.Reflection;
<!DOCTYPE html>
<html>
....
<footer>
<p>© 2017 - @Assembly.GetEntryAssembly().GetName().Name</p>
</footer>
</html>
I am not sure it is a good way, but it works for me :)
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With