ASP.NETコアWebAPIのサービスとしてAppSettingsにアクセスしようとしています。 Configuration.GetSection( "AppSettings")を実行すると、nullが返されますが、構成値[ConfigurationSet ":StorageConnectionKey:AccountName"]にアクセスできます。私は何が間違っているのか分かりません。サービスasp.netコアとしてのコンフィグレーションへのアクセス
マイStartup.csが
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Library;
namespace Athraya
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
// .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfiguration Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddOptions();
services.Configure<AppSettings>(options => Configuration.GetSection("AppSettings"));
// *If* you need access to generic IConfiguration this is **required**
services.AddSingleton<IConfiguration>(Configuration);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
}
}
}
の下に示されており、私のAppSettingは、私は私がしようとしています
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Library
{
public class AppSettings
{
public StorageConnectionKey storageKey {get; set; }
public CloudContainerKey containerKey { get; set; }
}
}
namespace Library
{
public class CloudContainerKey
{
public string ContainerName { get; set; }
public string FileName { get; set; }
}
}
namespace Library
{
public class StorageConnectionKey
{
public string AccountName { get; set; }
public string AccountKey { get; set; }
}
}
を必要なクラスを持っているライブラリプロジェクトを持っている
{
"AppSettings": {
"StorageConnectionKey": {
"AccountName": "myaccountName",
"AccountKey": "abc"
},
"CloudContainerkey": {
"ContainerName": "mycontainername",
"FileName": "db.dat"
}
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
ですコントローラで取得するには
public class ValuesController : Controller
{
private readonly AppSettings _appSettings;
public ValuesController(IOptions<AppSettings> settings)
{
_appSettings = settings.Value;
}
}
ここをクリックしてください。 IConfigurationインスタンスの使用を使用して、セットアップのAppSettingsへ
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
あなたがそのようなサービスを持っている場合、私はまだnullを取得します。設定(Configuration.GetSection( "AppSettings")); –
あなたは私のスタートアップを変更して、私が言及したように私がコントローラにアクセスできるように、どうすればいいと思いますか?私はこのブログのhttps://www.codefluff.com/getting-configuration-settings-asp-net-core-mvc/に従おうとしました。 ASP.NETコアで何かが変更されたことを私に教えてください。 –
@Vijayaravindは答えを更新しました。オプションクラスのpropertisの名前を、パラメータ名の設定と同じにする必要があります。 – Set