मैं ASP.NET कोर पर कॉन्फ़िगरेशन प्रलेखन के माध्यम से चला गया । दस्तावेज़ीकरण कहता है कि आप एप्लिकेशन में कहीं से भी कॉन्फ़िगरेशन एक्सेस कर सकते हैं।
नीचे Startup.cs टेम्पलेट द्वारा बनाया गया है
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsEnvironment("Development"))
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// 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.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
}
// 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.UseApplicationInsightsRequestTelemetry();
app.UseApplicationInsightsExceptionTelemetry();
app.UseMvc();
}
}
इसलिए Startup.cs
हम सभी सेटिंग्स को कॉन्फ़िगर करते हैं, Startup.cs में एक संपत्ति भी है जिसका नाम हैConfiguration
मैं यह समझने में सक्षम नहीं हूं कि आप इस कॉन्फ़िगरेशन को नियंत्रक या एप्लिकेशन में कहीं भी कैसे एक्सेस कर सकते हैं? एमएस विकल्प पैटर्न का उपयोग करने की सिफारिश कर रहा है, लेकिन मेरे पास केवल 4-5 कुंजी-मूल्य जोड़े हैं इसलिए मैं विकल्प पैटर्न का उपयोग नहीं करना चाहूंगा। मैं सिर्फ आवेदन में विन्यास तक पहुँच चाहता था। मैं इसे किसी भी वर्ग में कैसे इंजेक्ट कर सकता हूं?