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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Java String Templates Today
  • Managing Data Residency, the Demo
  • What I Learned From Crawling 100+ Websites
  • Building a Java Payment App With Marqeta
  1. DZone
  2. Coding
  3. Frameworks
  4. No ConfigurationManager in ASP.NET Core

No ConfigurationManager in ASP.NET Core

Where's my ConfigurationManager in my new ASP.NET Core application? Today, author Jonathan Danylko shows an easy way to access your settings in your new ASP.NET Core application.

Jonathan Danylko user avatar by
Jonathan Danylko
·
Jul. 18, 16 · Tutorial
Like (3)
Save
Tweet
Share
9.81K Views

Join the DZone community and get the full member experience.

Join For Free
dials, buttons, and knobs

with asp.net core 1.0 released, writing .net code is changing dramatically. i'm not going to say that someone moved your cheese, but rather deleted it.

configuration in asp.net core is a tad bit different from the configurationmanager.appsettings we're used to when grabbing application settings. in this post, i'll show you a quick way to set up your settings for your asp.net core application.

need some class-y settings

gone are the days of slapping something into a web.config and reading it. we need to bring some structure to our configuration. our configurations now use classes, so let's create our application settings class in a configuration folder.

configuration/demosettings.cs

public class demosettings
{
    public string maindomain { get; set; }
    public string sitename { get; set; }
}

we created some simple properties to hold our settings.

our appsettings.json will now look like this:

{
  "demosettings": {
    "maindomain": "http://www.mysite.com",
    "sitename": "my main site"
  },
  "logging": {
    "includescopes": false,
    "loglevel": {
      "default": "debug",
      "system": "information",
      "microsoft": "information"
    }
  }
}

now we need to explain our startup.cs file.

configuring the services

the startup.cs is where we define all of our settings for our application whether it be sql server, mvc, a plain file server, or whatever suits your fancy with asp.net core. the whole idea is that you are configuring your pipeline using middleware .

in our startup.cs file, you'll notice the configureservices method.

public void configureservices(iservicecollection services)
{

    services.addmvc();
}

for our custom configuration to work properly, we need some additional services.

public void configureservices(iservicecollection services)
{

    services.addmvc();


    services.addoptions();


    services.configure<demosettings>(configuration.getsection("demosettings"));


    services.addsingleton<iconfiguration>(configuration);
}

the addoptions() is the setup for allowing ioptions<t> to be injected into your code.

the configure method tells your configuration that we are defining a demosettings section in your appsettings.json file and it will hold the demosettings object data.

finally, we create the dependency injection for our configuration as a singleton.

injecting our options

our configuration is now available through dependency injection.

if we want to use a configuration setting in our controller (like the sitename), we need two things: a property to hold our settings and a constructor to perform constructor injection.

controllers/homecontroller.cs

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();
    }
}

the property configsettings will hold our settings in the controller, and once the controller is instantiated, our settings will be injected through the constructor.

as you can see in our index(), we pass the sitename over to the view through our viewdata (it would be better if it was passed using a viewmodel ).

conclusion

while the configurationmanager has disappeared for asp.net core applications, it's been replaced with something a little more decoupled and provides a more structured way of saving configuration settings for your application.

we demonstrated an easy way to pass your configuration settings around using depedency injection.

was this demonstration helpful? do you want to see more asp.net core 1.0 features? which features? post your comment below and let's discuss.

ASP.NET Core ASP.NET

Published at DZone with permission of Jonathan Danylko, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Java String Templates Today
  • Managing Data Residency, the Demo
  • What I Learned From Crawling 100+ Websites
  • Building a Java Payment App With Marqeta

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: