Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain the ASP.NET Core application name?

I would like do display in the footer of an ASP.NET Core 1.1

© 2017 MyApplication

<p>&copy; 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...

like image 638
serge Avatar asked Dec 06 '22 14:12

serge


2 Answers

You could try:

@using System.Reflection;
<!DOCTYPE html>
<html>
 ....

    <footer>
        <p>&copy; 2017 - @Assembly.GetEntryAssembly().GetName().Name</p>
    </footer>
</html>

I am not sure it is a good way, but it works for me :)

enter image description here

like image 65
Duran Hsieh Avatar answered Jan 11 '23 06:01

Duran Hsieh


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>

enter image description here

like image 40
Win Avatar answered Jan 11 '23 06:01

Win