Home Backend Development C#.Net Tutorial Detailed example of using appsettings.json configuration file in core Web (ASP.NET)

Detailed example of using appsettings.json configuration file in core Web (ASP.NET)

Apr 27, 2017 am 10:11 AM
asp.net core

This article mainly introduces how to use the appsettings.json configuration file in ASP.NET core Web. The article gives detailed sample code. Friends in need can refer to it. Let’s take a look.

Preface

Recently I have been studying the porting of asp.net programs to Linux. It just so happened that .net core came out, so I started studying.

The transplantation of the code was basically smooth, but I found that there is no ConfigurationManager in .net core, and the configuration file cannot be read and written. It was troublesome to write a separate xml, so I googled it and found a method, so I recorded it as follows, To facilitate future search:

The method is as follows

#Configuration file structure

public class DemoSettings
{
 public string MainDomain { get; set; }
 public string SiteName { get; set; }
}
Copy after login

Display effect in appsettings.json

appsettings.json

{
 "DemoSettings": {
 "MainDomain": "http://www.mysite.com",
 "SiteName": "My Main Site"
 },
 "Logging": {
 "IncludeScopes": false,
 "LogLevel": {
  "Default": "Debug",
  "System": "Information",
  "Microsoft": "Information"
 }
 }
}
Copy after login

Configuration Services

Original configuration

public void ConfigureServices(IServiceCollection services)
{
 // Add framework services.
 services.AddMvc();
}
Copy after login

Customization

public void ConfigureServices(IServiceCollection services)
{
 // Add framework services.
 services.AddMvc();
 
 // Added - uses IOptions<T> for your settings.
 services.AddOptions();
 
 // Added - Confirms that we have a home for our DemoSettings
 services.Configure<DemoSettings>(Configuration.GetSection("DemoSettings"));
}
Copy after login

Then inject the settings into the corresponding Controller You can use it

public class HomeController : Controller
{
 private DemoSettings ConfigSettings { get; set; }
 
 public HomeController(IOptions<DemoSettings> settings)
 {
  ConfigSettings = settings.Value;
 }
 
 public IActionResult Index()
 {
  ViewData["SiteName"] = ConfigSettings.SiteName;
  return View();
 }
}
Copy after login

Summary

The above is the detailed content of Detailed example of using appsettings.json configuration file in core Web (ASP.NET). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to enable Core Isolation's memory integrity feature in Windows 11 How to enable Core Isolation's memory integrity feature in Windows 11 May 10, 2023 pm 11:49 PM

Microsoft's Windows 11 2022 Update (22H2) enables CoreIsolation's memory integrity protection by default. However, if you are running an older version of the operating system, such as Windows 11 2022 Update (22H1), you will need to turn this feature on manually. Turn on CoreIsolation's Memory Integrity feature in Windows 11 For users who don't know about Core Isolation, it's a security process designed to protect basic core activities on Windows from malicious programs by isolating them in memory. This process, combined with the memory integrity feature, ensures

What does computer core mean? What does computer core mean? Sep 05, 2022 am 11:24 AM

Core has two meanings in computers: 1. The core, also known as the core, is the most important component of the CPU. All calculations, accepting storage commands, and processing data of the CPU are performed by the core; 2. Core, core is Intel's processor Name, Core is the processor brand launched by Intel after the Pentium processor. It has currently released twelfth generation Core processors.

How to Fix Processor Thermal Trip Error in Windows 11/10 [Fix] How to Fix Processor Thermal Trip Error in Windows 11/10 [Fix] Apr 17, 2023 am 08:13 AM

Most of the devices, such as laptops and desktops, have been heavily used by young gamers and coders for a long time. The system sometimes hangs due to application overload. This forces users to shut down their systems. This mainly happens to players who install and play heavy games. When the system tries to boot after force shutdown, it throws an error on a black screen as shown below: Below are the warnings detected during this boot. These can be viewed in the settings on the event log page. Warning: Processor thermal trip. Press any key to continue. ..These types of warning messages are always thrown when the processor temperature of a desktop or laptop exceeds its threshold temperature. Listed below are the reasons why this happens on Windows systems. Many heavy applications are in

.NET Core cross-platform application development practice: a seamless journey from Windows to Linux and macOS .NET Core cross-platform application development practice: a seamless journey from Windows to Linux and macOS Feb 26, 2024 pm 12:55 PM

With the launch of .NETCore, .NET developers have a new opportunity to easily write and run .NET applications on multiple operating systems. This article will delve into how to use .NETCore to achieve cross-platform application development, and share best practice experience on operating systems such as Windows, Linux, and macOS. 1. Prepare the development environment. To start cross-platform application development, you first need to prepare the development environment for each target platform. Windows On Windows, you can install .NETCoreSDK through Visual Studio. After installation is complete, you can create and run .NETCore projects through Visual Studio. Li

Is CORE coin worth holding for the long term? Is CORE coin worth investing in? Is CORE coin worth holding for the long term? Is CORE coin worth investing in? Feb 29, 2024 pm 05:34 PM

CORE coin: Is it worth holding for the long term? CORE coin is a cryptocurrency based on the Proof of Work (PoW) consensus mechanism and was founded by the Core team in 2018. Its goal is to establish a secure, efficient, and scalable digital currency system that is widely used for payment and value storage. CORE coin is designed to provide a decentralized payment solution that provides users with more privacy protection and transaction convenience. Advantages and security of CORE currency: CORE currency is based on the workload proof consensus mechanism and has strong security. Efficient: CORE coin’s transaction speed is fast and can handle thousands of transactions per second. Scalable: CORE coin has a large block capacity and can support a large number of transactions. Decentralization: CORE coin is a decentralized cryptocurrency

What is core under linux What is core under linux Mar 23, 2023 am 10:00 AM

Under Linux, core is a memory image with debugging information added. When a program exits or terminates abnormally under Linux, we will use the core file for analysis, which contains information such as memory, registers, stack pointers and other information when the program is running. The format is ELF, which can be understood as dumping the current status of the program into a file.

IFA 2024 | Core Ultra Series 2: In Lunar Lake, Intel introduces its most efficient x86 CPU yet IFA 2024 | Core Ultra Series 2: In Lunar Lake, Intel introduces its most efficient x86 CPU yet Sep 04, 2024 am 06:38 AM

Roughly one year after announcing the Core Ultra Series 1, also known as Meteor Lake, Intel follows up with the second generation. Core Ultra Series 2 aka Lunar Lake was already introduced at June's Computex. At IFA, the final launch of the Core Ultr

IFA 2024 | Core Ultra Series 2: With Lunar Lake, Intel introduces its most efficient x86 CPU yet IFA 2024 | Core Ultra Series 2: With Lunar Lake, Intel introduces its most efficient x86 CPU yet Sep 05, 2024 am 02:10 AM

Roughly one year after announcing the Core Ultra Series 1, also known as Meteor Lake, Intel follows up with the second generation. Core Ultra Series 2 aka Lunar Lake was already introduced at June's Computex. At IFA, the final launch of the Core Ultr

See all articles