Appsettings.json in .NET: How to read and get a value

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
app settings.json is used to save a configuration value so an asp.net core web app can read it this is part one of our app settings.json course and we'll have a look at how to read from the app settings.json file for asp.net core remember to hit the red subscribe button or go to youtube.com around the code to subscribe to our YouTube channel learn.net dependency injection and blazer webassembly with round the coats online courses go to aroundthecode.com courses now if you're as familiar with asp.net will be familiar with the web.comfig file we're going to have a look at the app settings now the problem we've got here is that when we set a value we have to set it as a string if we want to convert it to a particular type we have to do that within the application now these app settings represent an authentication section problem is we can't split it out into a section we just have to have the section as part of the key the other issue we've got is that XML adds quite a lot of code so we've got this app settings section here we've got an add keyword here we've got a key and a value attribute and if we have a lot of these settings it's going to add to the overall file size with asp.net core we have the accessing.json file as it's a Json file we can have different sections like what we've done with the Authentication in addition if we've got more than one value for a particular key we can add it as an array the final thing with this is that we can add different types so we've got a string but we can also add things like an integer or a Boolean in program.cs we can use the configuration as part of the web application Builder to do that I'm going to read the client Secret so I can call Builder dot configuration and I can use the get Value method within that we can add a generic type and this generic type represents the value that we're expecting so as a client secret is a string we're going to add that as a generic type now it's expecting a key parameter now what we do with this is we get the section which is authentication we add that in there we append a colon and then we get the client secret and add it to that we're gonna run a break point on that and run the application to see if it's outputting the value so we can see that the client secret string is outputting my secret and if we go to the app settings we can see that that represents the same value in the app settings file now I'm going to show you how to use the configuration in an asp.net core web API now I've got a home controller the first thing I'm going to do is I'm going to set a private read-only member of type I configuration and call it underscore configuration next I'm going to declare a Constructor passing in the eye configuration as one of the parameters as the configuration is added to the ioc container as part of the dependency injection it will have the instance as part of that I'm just going to set the private member to the parameter that we're passing in now I'm going to make some modifications to the index and what we're going to do is we're going to read all the app settings and output them as part of the response we're first going to add the client ID so we call the configuration dot get value expecting a type of string and we call it Authentication colon client ID as the key I'm going to do the same thing for the client secret as well so we're just going to modify the names now for the allowed host that's slightly different because it's a string array rather than an actual string so in order to Output a string array we once again use the configuration instance but this time we're going to use get section now get Section will use the same key format as for the get value so we're going to change that through allowed hosts and the final thing we have to do is we have to call the get method and in that we're expecting a string array so that's what we add the final thing we need to do is to add the attempts now the attempt is expecting an integer so we need to change the type for that so we will rename the names for it and we're going to change the type to int with the application running we can see that we're outputting the values into the response in the next part of our app settings.json course we'll have a look at how to add a connection string to app settings so it can be used with Entity framework and SQL Server check out the full course playlist at roundthecode.com app settings hyphen course
Info
Channel: Round The Code
Views: 19,082
Rating: undefined out of 5
Keywords: appsettings.json, configurationmanager appsettings, system configuration configurationmanager appsettings, appsettings, .net core appsettings, configuration appsettings, c# appsettings, get appsettings value .net core, read appsettings, get value from appsettings.json .net core, get value from appsettings.json c# .net core
Id: UiqTDvIFJ3g
Channel Id: undefined
Length: 5min 20sec (320 seconds)
Published: Sun Oct 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.