To setup a new .json configuration file perform the following steps:
1. Create a new file call "appSettings.json"
2. Type in the following markup in the appSettings.json file
{ "AppSettings": { }, "Data": { "NorthwindContextConnection": "Server=yourservername;Database=NorthwindCafe;Integrated Security=True;MultipleActiveResultSets=true" } }
The "AppSettings", and "Data" sections are the root section of the configuration. So if you want to access the "NorthwindContextConnection" you would type ["Data": "NorthwindContextConnection"] you can go as many levels deep as you want.
3. Now we need to setup the Startup.cs file to recognize the new appSettings.json configuration file
at the top of the Startup class declare a static variable call Configuration
public static IConfigurationRoot Configuration;
The IConfigurationRoot is in the Microsoft.Extensions.Configuration
4. Now create Startup constructor to add the appSettings.json file, like the code below
public static IConfigurationRoot Configuration { get; private set; } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appSettings.json") .AddEnvironmentVariables(); Configuration = builder.Build(); }
The adds the appSettings.json file in the root directory of the application and then builds the key/values information into the Configuration object.
5. Now go to the ConfigureServices method and right below the services.AddMvc() method call, type in the following
var connectionString = Startup.Configuration["Data:NorthwindContextConnection"];
The complete method should look like the following:
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); var connectionString = Startup.Configuration["Data:NorthwindContextConnection"]; }
If you put a break point where the connectionString line is you will see that the connection in the appSettings.json file has been retrieved
Mua vé tại đại lý vé máy bay Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ bao nhiêu
các chuyến bay từ mỹ về việt nam
các chuyến bay từ nhật bản về hà nội hôm nay
bay từ đức về việt nam mấy tiếng
lịch bay từ canada về việt nam
Lịch bay từ Seoul đến Hà Nội
chuyen bay chuyen gia ve viet nam
When I first dove into ASP.NET Core and needed to set up appSettings.json for my database connection, it felt like a big shift from the old web.config days. Balancing this with other tasks became overwhelming. That’s when I discovered take my online exam. They handled the complex setup for me, freeing me to focus on other important aspects of my project. If you’re struggling with similar tech challenges, this service could be a real lifesaver!
ReplyDeleteGyms in Georgetown offer a diverse range of fitness facilities and services catering to various preferences and needs. The area is home to numerous establishments, each providing unique workout environments, from large commercial gyms equipped with state-of-the-art machinery to boutique fitness studios that focus on specialized classes such as yoga, Pilates, and high-intensity interval training.
ReplyDeleteLocal residents can enjoy engaging workout spaces that encourage community interaction, personal training services for tailored fitness experiences, and an array of group classes designed to keep participants motivated and accountable.