DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Implementing Effective Document Fraud Detection in C#
  • A Guide for Deploying .NET 10 Applications Using Docker's New Workflow
  • SHAP-Based Explainable AI (XAI) Integration With .Net Application
  • Why Architecture Matters: Structuring Modern Web Apps

Trending

  • Building a Practical Cloud-Native Golden Path: A Guide to Kubernetes-Based Service Delivery, Self-Service, and Developer-Friendly Defaults
  • Going All In On the JVM 25 Virtual Threads
  • Build Software Faster With Three Simple Principles
  • Two Engines, One Query: DuckDB Inside Neo4j

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Read Configuration in the Controller: .NET Core Quick Posts

Read Configuration in the Controller: .NET Core Quick Posts

You can find my all .Net core posts here. In this series of posts, we will see some quick tips or tricks to use some specific things in .Net Core In this pos...

By 
Neel Bhatt
·
Updated Aug. 24, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
40.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In this series of posts, we will look at some quick tips or tricks to use in .NET Core.

In this post, we will see how to access configuration in the controller.

As we know, in .NET Core we have an appsettings.json file to store the configuration related data.

For example, you have an appsettings.json as shown below:

{  
   "Logging":{  
      "IncludeScopes":false,
      "LogLevel":{  
         "Default":"Warning"
      }
   },
   "myFirstKey":"myFirstValue"
}

From .NET Core 2.0 and on, you are no more required to add new IConfiguration in the Startup constructor. Its implementation will be injected by the DI system and is already available in Startup.cs class:

Image title

But if you are targeting a .NET Core version lower than 2.0 then you need to add IConfiguration in Startup.cs.

Access Configuration in Controller

We can directly inject IConfigurationService into the controller as below:

public class HomeController: Controller {
 private IConfiguration _configuration;

 public HomeController(IConfiguration Configuration) {
  _configuration = Configuration;
 }
}

That is it. You can now access the config from the HomeController.

For example, if you want to use it in a Contact action, then access it as shown below:

public IActionResult Contact() {
ViewBag.ConfigValue = _configuration["myFirstKey"];
}

As you can see below, myFirstValueis the value of the key myFirstKey in the appsettings.json file.

Complex Config Structure

You can even have some complex structure in appsettings.json as below:

{  
   "MyConfigSection":{  
      "MyFirstConfig":"Neel Bhatt",
      "MySecondConfig":{  
         "MyFirstSubConfig":27,
         "MySecondSubConfig":"Pune"
      }
   }
}

To access this, write the below code:

public IActionResult Contact() {
 // Option 1: Configuration using Key string
 ViewData["MySectionMyFirstConfig"] = _configuration["MyConfigSection:MyFirstConfig"];

 // Option 2: Configuration using GetSection method
 ViewData["MySectionMySecondConfigMyFirstSubConfig"] = _configuration.GetSection("MyConfigSection").GetSection("MySecondConfig").GetSection("MyFirstSubConfig");

 // Option 3: Mixture of Option 1 & Option 2
 ViewData["MySectionMySecondConfigMySecondSubConfig"] = _configuration.GetSection("MyConfigSection")["MySecondConfig:MySecondSubConfig"];

 return View();
}

Read the Connection String

To read the connection string, you need to call it using the GetConnectionString method as shown below:

var connectionString = Configuration.GetConnectionString("myConnectionString");

Hope this helps.

You can find my all .NET Core posts here.

.NET POST (HTTP)

Published at DZone with permission of Neel Bhatt. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Implementing Effective Document Fraud Detection in C#
  • A Guide for Deploying .NET 10 Applications Using Docker's New Workflow
  • SHAP-Based Explainable AI (XAI) Integration With .Net Application
  • Why Architecture Matters: Structuring Modern Web Apps

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook