【发布时间】:2016-09-11 04:49:45
【问题描述】:
使用 ASP.Net 5 MVC。看到这个问题跳来跳去,但不是一个完整的答案。我想要做的是有一个能够访问 AppSettings 的助手类。我可以在控制器和视图中访问它,但还没有弄清楚如何在我自己的自定义类中访问它。像这样配置启动。
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}
.................
.................
【问题讨论】:
标签: c# asp.net asp.net-core-mvc