Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:10Length:1.5 hours
  • Overview
  • Transcript

2.2 Building an App From Scratch

You can build an ASP.NET Core app from scratch using the built-in command-line tooling. In this lesson, learn a bit about a basic ASP.NET application and how it executes.

2.2 Building an App From Scratch

Our first application is going to be very, very simple. In fact it's going to be very, very useless. But it is going to give you an idea of what ASP.NET Core applications are. As well as how they run across multiple platforms. So we're going to start with the command line and by the way I'm on Mac OS but that doesn't matter. You can run the same exact commands, use the same exact code and you will get the same exact results. Be it if you're on Mac OS, Linux, Windows or any other supported platform. That's the beauty of cross-platform development that we finally have with .NET. Okay, so the first thing we need is a folder that's going to contain all of our code. So this is going to be our projectfolder. So, I'm going to call this firstapp. And then, we want to cd into that directory. And then, we're going to use the .NET command line interface. Every .NET command begins with .NET. So we say dotnet and in this case we want to create a new project. So dotnet new. And this is going to give us two files. We have program.cs which is our code. And then we have project.json. Which is a file that contains all the information about our project that the compiler needs in order to compile our application. Now there's another command called dotnet run, which we could do, but it's not going to work because it tells you why. It says that it's looking for a file called project.lock.json which doesn't exist. And it tells us that we can fix that by running dotnet restore. We're not going to do that just yet. Instead we're going to open up code and we're going to look at these two files. Now project.json unfortunately is going to go away. And that's heartbreaking for anybody that has been using .NET Core since, well, its inception. We love the JSON format. But the problem here is that the .NET team is moving .NET Core to use MS Build in order to compile our applications. And MS Build uses XML configuration files. So for the sake of simplicity as far as maintenance is concerned, on Microsoft's end. They are making .NET Core use the good old .csproj files. .csproj, which are XML files and nobody's happy about that but that's how it is. And hopefully we will get JSON back but it's going to be a while. So we're not going to really spend a whole lot of time talking about project.json because it's going to go away. But the same concepts are going to be used there. Now if you're using code you are probably seeing these two things at the top. The first is asking if we want to bring in the assets for debugging our application through code. This is strictly for Visual Studio code, and in this case I'm going to say not now, because we aren't going to debug this application. Now, the second thing says that there are some unresolved dependencies from project.json. Basically, it sees that we have some dependencies here. They haven't been actually loaded for our project. Do we want to load those? Yes, we do, but we aren't going to do that here. Instead we're going to do that from the command line. Now you can click Restore if you want to and then see me do it from the command line. It doesn't matter. You're going to get the same results. But let's hop on over to program.cs. Now this is our C# code and you can see that there are a bunch of red squigglys. The reason being because there are some dependencies that we need to restore. And whenever we restore those, Visual Studio Code is going to see that those dependencies are there. So then it's going to get rid of all of those red squigglies. So if we go back and when we say dotnet restore that's going to restore all of those packages. It's also going to create that lock file. So if you wanted to look at that lock file which we should have seen. There it is. This lists all of our dependencies that are being used by our application. And you can see that there's a lot more here than what is defined inside a project.json? So, basically everything that is currently in the lock file is part of Microsoft NETcore App. And we're also importing dnxcore50. So, that's what that lock file looks like. And once again this is going to be going away, so let's just not talk much about that. So, let's go back to our code. All of the red squigglies have gone away. And you can see that this is a console application. Now, if you're brand new to .NET development. This is what a normal console app looks like, there is a namespace. Then there is a public class called Program that has a static method called Main. That is the entry point for our application. And then it writes something to the console. So we can go back to the command line. And we can say dotnet run and it's going to compile our application and then it's going to run it. So we just ran a .NET application on Mac OS and I think that that's awesome. So hello world. That's all that this application does. So let's add some interactivity to it. Instead of saying WriteLine, we're going to say WRite. And we're going to prompt the user to insert their name. So let's say just enter name, or Enter your name, rather. And the reason why I changed this to Console.Write. Because if you write line then it's going to put a carriage return at the end of this line. So we want the user to input their name on the same line. So we're saying Console.Write. Then we want to read the console so we will say Console.ReadLine. That's going to take everything that the user types before they press the Enter key. So we will have their name and then we will simply write back their name, but we'll say hello. And then name. So it's very, very simple. But we at least have some interactivity here. So we'll go back and we'll say dotnet run. It's going to see that our program.cs file changed. So it's going to recompile the application and we can see that we have a different thing here, Enter your name. My name is Jeremy and so we see Hello Jeremy, and then our application exits. So let's take this console application and turn it into a web application. Now you might be thinking, how do we do that? Those are two completely different things. And yes, you're right, except you're also wrong. Because every .NET Core application is a console application. Now I know that that doesn't make a whole lot of sense but when you think about it, it does. Because the easiest way to make something cross-platform is to just make it use the console. Yes, Microsoft could have made .NET Core so that you could write graphical applications. But then you would also have to think about what would go into making that possible. Not only would Microsoft need to do what they've done with the full blown version of the .NET framework. Which is tie it to the Win32 API. But they would need to do the same thing on a Mac OS, on LInux. And then on Linux, well, you have a ton of other options that people could use for their UI. So, from a realistic standpoint, it's a lot easier to make something cross-platform if you're just targeting the console. So every .NET Core application is a console app. That means that our application here is going to have to spin up a web server so that we can then respond to every request. So, the first thing we want to do is go to project.json and we're going to add a dependency here. It is called Microsoft.AspNet Core and then server and Kestrel. Kestrel is this cross-platform web server that we can use to run our application. And we're going to use version 1.1.0, that matches the run time that I installed on this machine. So we have this dependency. And now Visual Studio Code says that, hey, there's a new dependency. Do you want to bring that in? Yeah let's go ahead and do that. We could do that from the command line if we wanted to, but it was prompting us to do so. So you just might as well have done that now. Okay. So inside of our code file, we need a few using statements. The first is going to be the builder namespace. And that is Microsoft.AspNetCore. And Builder. This is going to give us access to a class so that we can build a web host. And we might as well just copy that line because we're going to need the majority of that on our other using statements. The second thing that we want to bring in is the hosting namespace. And then we also want the http namespace. So we have these three things. And so inside of our main method, we're going to create a web server host basically. We're going to configure it and it is going to respond to requests. In fact, let's go ahead and write that method that's going to handle the request. So we need to make this static so that we can reference it from inside of our main method. And this is going to be asynchronous. So it's also going to return a task. Now we could return void. Although, no, I don't think we could. I think we need to return task here. And then let's just call this HandleRequest and this method is going to accept an object called an HttpContext. This is basically going to contain everything about the request. But it also gives us access to the response, as well. So, if we wanted to get any information from the request we would say context.Request. And then we have many properties and methods for working with request. But, in this case, we are simply going to respond back by writing some output. And we are essentially going to do the same thing that we did when we wrote to the console by saying hello and then the person's name. So we're going to write Async. We're going to paste in that code, but we also need access to this name variable. So the easiest thing to do is to just have a private static string field called _name, so that we can use that and have access to it. So, let's make the necessary adjustments. And there we go. Now we do need a using statement so that we can use the task data type. So if you hit Cmd.on task, it's going to give us several options. If you're on Windows, just hit Ctrl. And you will get the same thing, essentially. It'll look a little bit different. But we want to add a using statement for System.Threading.Tasks, so we will do that and then that goes away. So now inside of Main we just need to build our web host, configure it, and run it. So we are still going to write to the console Enter your name. We're going let the user type in their name but then we're going to show the result in the web browser. So we want to create our web host, and we do that with a class called WebHostBuilder, which is aptly named. This is for building a web host. And because we are using Kestrel, we have an extension method simply called UseKestrel. So that our web host is going to use the Kestrel Server and then we want to configure our host. So we call the Configure method. And this is going to accept an object called an application builder. And we're basically going to say that we want to run our application and we specify the method that's going to handle the request. So this is where we use our HandleRequest method and then finally we say Build. So we create our web host builder. We are using Kestrel. We are configuring our web host so that for every request we are going to use this HandleRequest method. We are building that. And then finally, we're going to run our host. So let's save this. We're going to hop back over to the command line and we will say dotnet run. It's going to recompile the application and then it's going to prompt us for our name. My name is Jeremy, and then we see that we now have a server running at localhost port 5000. So, let's hop on over to our browser. We want to go to localhost:5000 and here we see the results. Hello Jeremy. So ASP.NET Core apps are console apps and you can build them without using any third party tools. But that's really only useful if you plan on writing a very simple application or you plan on writing your own framework. But in most cases and dare I say all cases, you will want to use a pre-built framework for writing your web apps. And that's where the MVC framework comes into play. And we will start looking at that in the next lesson.

Back to the top