Get Values from AppSettings.Json (.NET5 / .NET Core)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys it's chris today i'm going to talk to you about the iservice collection ad options and how you can use that in your application to read the appsettings.json take the data out and use it like you would with any other data from a class so i hope you like this content we'll jump right into the code i'm just real quick if you do like it at the end please go ahead and click that alarm bell down below and subscribe for me i do appreciate your support and i hope you enjoy it we'll see you all right let's create a new project i'm going to do this project in mvc for core but you could really do um almost anything you want and get the same result and still have you know some value out of this as long as you pick that.net project let's just go ahead and pick a dot net core web application next that looks good to me and we're going to do mvc but like i said you could pick one of these other web applications and you could still do this create all right so the first thing you're going to want to do is go ahead and put some information in your app settings so i will warn you going multiple levels out it doesn't really like that and i looked in the microsoft documentation it literally said separation of concerns like they don't want you to do that so we're just going to keep it at like the two levels for now um so why don't we create you a custom one that says my my settings curly braces and let's do my uh how about abc it has the value of hello world i know you developers don't get sick of hearing hello world right in tutorials let's do def and we'll give it to uh we want to say goodbye world that sounds horrible right and oh i remembered this time i zoomed in you're welcome so let's say hello again i zoomed in yay all right so moving on now we're going to go to startup.cs and in here we're going to register a service so scroll down here to add good services that add controllers with views and right under that you can do services dot add options and um just i'm going to put a comment here to tell you what this really is which is i options t if you hover over it it'll show you that it's iservice collection ad options all right okay so just above that and you may have other things in here so it's just remember it's going to be at the top of your configure services method you can add another one it's going to be services.configure configure and in here you're going to put the name of your class which i know we haven't created this class yet but we're going to be creating a class called my app settings and we'll do configuration dot get setting section you can just reuse this here again so my app settings dot and remember this one here section name i just pasted that in there for you section name now it's all underlined in red because we haven't created it yet but we'll do that next now let's go ahead and build a class all right so i like to create another folder here i open the folders up so you can see them but i usually like to add a folder called services and dot net core or done at five you know people usually separate out the interfaces from the concrete services and the two separate folders within that so you could do like add transient and your startup.cs and right register a bunch of custom services that you could then inject into the controller's and into other services etc but in this case since we don't have to build an interface for it we're just building a class i'm just going to just use a services folder without the two subfolders so let's go ahead and create the class and my app settings very nice all right so now within this in here we're going to do public const string section name equal to and here we're going to pop into that app settings json and get the name of that header right there just like that i copied that and we'll go back to our new class here and i'm gonna put it right in here now we can grab anything that's underneath that level and we'll put them in here as properties all right so let's just remember what they were again so abc and def and they're both strings but i'd like to use something that's another type zaynager or something x y z 995 okay don't ask me where that number came from it just popped in my head all right so now we can do uh public string and it's a property so public string abc get set i'll do the easy way prop tab tab string tab def let's do it again prop tab tab in tab xyz all right it's off the screen all right so now we want to use this i'm sure you all have used services before but in case i'm i'm lucky enough that you haven't then i get to teach you another thing as well so let's go ahead and uh inject this service um that we defined in our startup uh that we requested into our home controller as you can see home controller comes with already the logger service so we're just going to add another one right under it in the same fashion option eye options and of type and this is where we just put the name of the service we just created so my app settings and put that in the clipboard and that's shift alt enter if you want to go full screen like that and then just call it whatever you want and i and i just follow the same kind of uh way of wording things that the defaults templates use which is underscore i think they've done that since back in the day but if you do control dot it will give you some options here and just choose using microsoft.extension.options that's that is the namespace that that interface lives in and then control dot here and it's going to pick the namespace that your web application shows and in case you don't know this the reason why your name space here is web application one dot services is it's nothing fancy it's just but it's in the services folder you don't have to keep it this you could delete this you could add to the end of it it really doesn't matter it's just a unique name so ctrl shift s and i'll save all my files you'll notice that all the three stars on the top just went away because all three files just got saved and now let's come back here and in the same manner that they injected this we're going to say that this controller in its constructor expects a class that implements i options of my settings and we can just call it um options that sounds good enough to me now here let's let's get our local variable assigned to the class our private read-only to what came in in the constructor and so now here in the index we could do something with these variables so i could set a breakpoint here or i could put them inside a view bag to return to the model for example so you just do like do bag.myvar1 equal to and now you can just grab that options dot value always says dot value okay and boom there they are abc do a couple of them d e f and x y z and if you hover over it you can see that last one's an int so we know what these are we're going to show them in our view now let's go over to the view and maybe we'll drop the line here and put them in a paragraph and use the add sign to get up to do bags one two and three and i'm gonna go ahead and launch my application with my control plus shortcut oh build errors oh so we never gave it the name space so ctrl dot using the name space the error went away uh and um there is one other thing actually i just realized app settings json you should always double check that you formatted this correctly because i did this earlier and i was missing this comma here so just make sure that you have proper json file here and sometimes it'll be a little green line if it's not all right so we'll go ahead and launch our application and step through it hello world hello again i zoomed in oh we should zoom in some more and then if you hover on this third one null now it's 995 because i had at the f10 over the line hit f5 to launch the view hello world i zoomed in 995. pulled right from our config file all right now there are ways to pull them directly into a class but since we're showing off mvc here i think that the service was a better way to show it to you um i will post a link to the official documentation in the video if you want to learn how to do just a real basic loading into a class it's just about the same amount of code as what i just did just a little different and there are some other classes that are similar are other interfaces that are similar but the main difference is just the time that they pull them the time that the data is pulled from app settings at json whether it's pulled throughout the application or just when the app's loaded that kind of thing the life cycle and that'll all be in the official documentation the same webpage i send you it's a really short document i suggest you take a look at it if you're going to go ahead and use this well thank you so much for watching have a good day
Info
Channel: Data Vids
Views: 9,611
Rating: undefined out of 5
Keywords: appsettings.json, .net5, dotnet5, dotnet core, .net core, serializing appsettings.json, asp.net mvc dotnet5, asp.net mvc core, asp.net mvc appsettings, reading config values, extract data from config, project properties, c#, csharp
Id: q6oSmaWWjqc
Channel Id: undefined
Length: 12min 27sec (747 seconds)
Published: Thu Feb 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.