2017-07-24 28 views
1

我想请勿在ASP.NET 1.1的核心如何获取ASP.NET Core应用程序名称?

© 2017年MyApplication的

<p>&copy; 2017 @(ApplicationName)</p> 

如何获取应用程序名称的页脚显示?

我发现an article关于这个问题,但它是关于PlatformServices.Default.Application.ApplicationName混乱,因为它说,不使用Microsoft.Extensions.PlatformAbstractions,但并没有说,而不是使用什么应用程序名...

+0

而不是将其直接连接到组件的名字,我想一个更好的解决办法是干脆把你想要的确切的文本在布局文件。 –

+0

@NateBarbettini我想要的确切文本是在项目属性中填写的应用程序名称。 – Serge

回答

1

你可以尝试:

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

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

我不知道这是一个很好的方式,但它为我工作:)

enter image description here

+0

应该是什么好方法,把应用程序名称放在资源文件中? – Serge

+0

仅适用于应用程序名称,因为应用程序名称并未经常更改,所以我宁愿在布局文件中放置所需的确切文本。 :)顺便说一句,开发人员直观地了解它在哪里。 –

1

有很多方法可以实现它。这是我如何在我的项目中做的。

我通常有不同的应用程序名称,可能有空间或更长的项目名称。所以,我在appsettings.json文件中保留项目名称和版本号。

appsettings.json

{ 
    "AppSettings": { 
    "Application": { 
     "Name": "ASP.NET Core Active Directory Starter Kit", 
     "Version": "2017.07.1" 
    } 
    } 
} 

Startup.cs

appsettings.json文件到AppSettings POCO负载设置。然后它会自动在DI容器中注册为IOptions<AppSettings>

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddOptions(); 
    services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); 
} 

AppSettings.cs

:我有一些其他的设置,使我把它们放在一起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)

进样IOptions<AppSettings>查看。 如果您愿意,也可以将其注入控制器。

@inject IOptions<AppSettings> AppSettings 

<footer class="main-footer"> 
    @AppSettings.Value.Application.Version 
    @AppSettings.Value.Application.Name</strong> 
</footer> 

enter image description here

相关问题