與ASP.Net MVC 5玩弄。看到這個問題跳過,但沒有一個完整的答案。我想要做的是有一個輔助類,它可以訪問AppSettings。我可以在控制器和視圖中訪問它,但還沒有想出如何在我自己的自定義類上訪問它。啓動配置如此。應用程序設置的自定義類ASP.Net MVC 5 6
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"));
}
.................
.................