ASP.NET Fundamentals - Environments (how to use environments)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
greetings everyone in this video we're going to take a look at net fundamentals environments we'll also go through a quick hands-on lab exercise so let's get started so today we're going to take a look at what are environments we'll also look at what dot net provides us to complement environments and within that looking at how to access that information via dependency injection as well as leverage that via tags within our presentation code and throughout it all we'll take kind of circle everything together and perform a lab and a demo exercise so first let's start with what are environments well most of the time in your development pipeline you're generally going to separate out uh development into several sections or stages in which you want to promote code through that's maybe not quite validated all the way to where it's ready to go live most of the time you're going to have things like a development where most of your software development literally occurs the software development team is working and they're deploying their code to a development environment for integration the staging environment oftentimes is where qa is going to perform testing or users are going to perform user acceptance testing prior to having it certified for deployment to production sometimes you're going to have more of these sometimes you may have just a few but most the time you're going to have a few of these and microsoft provides support for those natively oftentimes you're going to want to have to change configuration per environment you may not have as powerful as system as in development as you do in staging and then prod so you may want to have a different configuration to take advantage of that different system performance there's often to other times that you'd want to use configuration as well in addition to configuration it gives you a way to isolate these changes or a new version of your software from one stage or environment to the other and honestly it's all built into dot net so why not use it it's already there so that's what we're looking at today most of the information that we're going to be speaking about and looking at in our lab is literally taking from microsoft documentation and you can easily find that um if you go out to microsoft's site to the docs we're in asp.net docs fundamentals and environments almost everything we're going to talk about is right here and with that said how do you set that environment right if you're in visual studio you can easily set that with a environment parameter in visual studio and we'll look at that in our exercise also if you're going to be a system administrator and setting this at the server level you can set this up as a system environment variable on a windows computer either.net underscore environment if you're running any type of.net app or if you're just running asp.net apps you'll want to run asp.net core excuse me asp net core underscore environment and like we talked about before the native ones are going to be development staging and production and you'll see why here in just a moment when we look at a little bit of code however you can still have your own custom environment name so let's take a look at how we'd access this in dependency injection when we look at our lab exercise a little bit later it's it is pretty much a standard file new asp.net mvc project and when you create those type of projects in the startup cs file there's a configure method and in the example that we're going to look at is very similar to this top section here where the iweb host environment is injected into that particular method and you can do things with it and that represents the env variable there you see the dot is environment is development excuse me the dot is production and dot is staging well the custom environment here staging two that's something that you can add any one of your own custom names and that's how you would access that now while we're on the topic of startup.cs if you find yourself in cases of having to do a lot of these kind of if and if statements where you have a certain startup configuration specific to development or specific to staging there's a way to actually create separate startup classes to be used per environment and i would recommend that you would reference the documentation for that i think it's in the lower section of that that we referenced outside of dependency injection and that's obviously your code behind you could access that in you know virtually any class there in your asp.net project as well as uh within a controller or wherever or even do an injection on the view if you really wanted to but why would you do that when you have an environment tag helper and that's kind of the beauty here is these kind of represent your if and else statements right so think of the first one as if environment equals development show me this div tag that's kind of what it is and the second one is if the environment is not development then show me this other dave dag so it kind of represents the similar thing what we saw in the previous score with the ifs and else's but you're seeing these here in tags and html tags getting rendered so that's a great advantage we'll look at this a little bit further in our lab speaking of which go ahead and go to github.com brad the coder samples.environments and download the repository there clone that repository and let's go ahead and open up the solution let's take a look at some of the actual configuration settings where you would set the environment first with the if you're in visual studio you can do on your mvc project if you go to properties and then we go to the debug tab here this is where it's set to asp core underscore environment development now this this will be used for visual studio only but it's very similar to when we set this on the server so if you are a server administrator or wanting to set it for the entire computer you can actually get a windows machine right click on the task button there the start button excuse me and go to system and then here on the right go to advanced system settings and then here's our environment variable so you would set up either the net underscore environment or the asp.net environment as the new variable and then either development staging or production for the value and that will set up your environment now based on that now we'll take a look at what our code is going to do so we have a couple of examples we're going to be walking through so let's just kind of do an f5 and see what our example looks like and if we look at it similar to if you've seen the configuration video very similar we have our environments this is the link to the documentation we've been following for our lecture portion uh it's available and then our examples are going to be we're going to access it through the controller using dependency injection and then we'll use the tag helper to display something on on this this razor page here so first and foremost in the controller we just have a simple message welcome to the development environment we go back our tag helper and it's just displaying that because we are in development that qualifies that we included development in uh i'll look we'll show look at the code here in a minute for this the lab exercises are going to be very similar we're essentially going to mimic what we do in the examples but we're going to do these on our own uh so we're basically going to use dependency injection here to display a message that's unique and then we'll use a tag helper here to display this in our lab so let's go ahead and look at the code now that we've seen what the app does so first off let's take a look at our controllers and we have our example controller first again we're going to do some dependency injection here that was the iweb host environment we had talked about in our lecture portion and we're going to inject that in the constructor here and we've got a couple of controllers obviously our tag helper is just going to return that view but in the access controller we're just going to we have just a little simple view here that has a message property that we're going to populate and in this case based on that some of these native options and you know if you ever you know don't have this example here when you do a file new mvc project uh if you go into startup in the configure this is where that default example always exists here it's doing a injection here as well as just giving you an example of the environment option and that's kind of what we're going to exploit in ours is we're using development and staging and let's say we wanted to have another different one that was the named instance so we could have a staging two uh which would display this we're gonna use the same message but you can see it qualifies for both uh production so staging development and production are standards but you can have any of your own custom ones as well as we talked about in our lecture portion and that's pretty much it so we're hydrating it there so let's take a look at both of the views so that associated view is very simple we're essentially going to take that model and populate the message no big deal similarly with our tag helper now let's take a look at it so with the tag helper this is our tag and we have this include exclude option with comma separated values so in this case we have include development so if it is the development environment we want to display this message and if it is not the development environment we want to display that message and there's this is including three so it if since we saw this on our screen we had a and a second message here which we saw and if it's production we'll see that production message so as you saw we were already set up as our development environment so let's do another example of staging two as we saw so staging underscore two we'll save that and let's launch that and we'll see our message change on those two examples so now if we go to there's our staging environment as we expected and on the tag helper we saw it's not development right excluded and it's staging two there so this that's why both of those tags are displayed perfect works great all right let's go ahead and just temporarily set that back and we're going to begin our lab exercise now so with the lab exercise what we want to do is essentially repeat the exact same thing uh that we saw but with our own exercise and instead of having to to for you guys to see me type all that code we do actually have another feature branch available to you with that completed exercise in it so if you look at that exercise let's take a look first at the controller so if you looked at it before it didn't have anything and so we had to uh in this case we're going to inject our method just like we did in the extra the example and we're going to create our own lab model there and similarly we have a message so we're going to do a message property on all these here right and give them all a message and then on top of that we're going to go ahead and in our view we have two different exercises remember so we're just going to take that model and display the message no big deal right and then on our tag helper we want to just kind of whatever we can do is for development staging production to show a unique message here that we are in one of those environments one way or the other right so in this case it's real simple including it you can get more advanced but that's essentially it so let's take a look at that launched and look at those lab and remember we set it back to development so we should see development messages all right access controller there's our unknown and our tag helper also didn't work so let's see where we went wrong let's take a look at our environment variable ah we spelt development wrong ah interesting see that simple change can make a big difference right so if you have your uh environment set up incorrectly or have a wrong one that can directly impact now hopefully this would be set on the server and once it's set and validated you don't have to worry about that but perfect example all right development environment worked as expected development environment worked as expected all right i hope you had uh learned some good things from this environments uh tutorial if you have any questions or comments please leave them in the comment section below thanks for watching don't forget to like subscribe and sign up for notifications and i'll see you in the next video here's some other videos you might like
Info
Channel: bradthecoder
Views: 75
Rating: undefined out of 5
Keywords: aspnet environment, dotnet environments, asp.net environments, development, staging, production, asp.net fundamentals, .net environments
Id: 2UxVk25Y0Jc
Channel Id: undefined
Length: 13min 45sec (825 seconds)
Published: Sat Jul 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.