Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

Cancel

Free Preview: Get Started with ASP.NET MVC 6

Introduction

01:12
  • Overview
  • Transcript

ASP.NET is used by millions of developers worldwide to build web apps. With each release, Microsoft changes ASP.NET for the better and version 5 with ASP.NET MVC 6 is no exception. Microsoft says that this upgrade makes "ASP.NET leaner, more modular, cross-platform, and cloud optimized."

In this course, you'll learn everything you need in order to get started writing web applications with ASP.NET 5. I'll show you how to create your first app using the ASP.NET MVC 6 framework. We'll see how to use some of the new tooling for web development, how to implement a web API for consumption by other programs and more, all in the context of building a simple message board app.

It's never been a better time to get started with ASP.NET. Learn ASP.NET Core and develop and run ASP.NET applications on Windows, macOS, and Linux. Check out our course: Start Coding With ASP.NET Core.

1. Introduction

1.1 Introduction

Hi there. My name is Jeremy McPeak, and in the next few hours I'm going to teach you everything you need to know in order to get started with ASP.NET MVC 6. This is a very exciting time to be an ASP.NET developer, because Microsoft is making a lot of wonderful changes to the platform. Now, don't worry if you don't have any existing experience with ASP.NET. I will teach you MVC 6 from scratch. All you need to know is C#. And you'll need to download some version of Visual Studio 2015 from visualstudio.com. Now, if you go to the Downloads link, you're going see Visual Studio Community, Ultimate, and Code. Now, I use Visual Studio Community in this course, so you can download that. It is completely free. Just install it, follow the instructions, and you will have Visual Studio 2015 installed on your computer. Now, bear in mind that as I'm recording this course, ASP.NET and Visual Studio 2015 are still in beta. However, the platform is pretty stable, and you will learn what you need in order to get started with ASP.NET MVC 6. So when you're ready, queue up the first video and we'll get started.

2. Getting Started

2.1 Touring an ASP.NET MVC 6 Project

ASP.NET 5 brings a lot of drastic changes to the platform, and I'm not going to detail what these changes are because that's not what this course focuses on. However, I mention this because if you are familiar with previous versions of ASP.NET, well then you're going to see a lot of changes. For example, let's create a new project, and make sure that you have web selected over in the left hand side. And there are three things listed here. Now the first item is ASP.NET's Web Application. This is something that really isn't new. We've been using this particular project template for the past few versions of Visual Studio. However, there are two new things listed here. There's this class library, and then there's this console application. And of course, you can see that they both have this vNext icon. Well, the class library is something new. It's more than just a DLL l that you would add to your application, because now ASP.NET is completely modular. There's this core execution context that is required in order to run ASP.NET. But anything other than that core requires NuGet packages. So if you want to create an MVC application then you have to include the MVC NuGet packages. And that's what this class library is. This is so that you can create a class library that you would then include inside of your new ASP.NET 5 applications, and then the Console Application seems kind of weird to be underneath the web projects. But this is so that you can create a Console Application that runs an ASP.NET application, so you don't have to have IIS anymore. ASP.NET is completely separated from IIS. So, that means that you could run ASP.NET from a console or you can take an ASP.NET application, put it onto an Apple computer or a Linux computer and run it from there as well. So, lots of things are changing as far as how we create applications, but the changes aren't just here. Let's go ahead and let's keep this name of web application one, and then let's go to the next window. And this is going to be somewhat familiar if you have been developing with ASP.NET, or at least if you've been looking at it. Now the top portion are the ASP.NET 4.6 templates. We can create an empty website. We can create a web forms website, NVC, Web API, and you can see the other things. Now the only thing that you can't create from this template is a web pages application. In order to do that, you have to go to File > New > Website. But things are once again changing as far as ASP.NET 5 is concerned. Because if you want to create a web pages application or a NVC application or a web API application, well, you just choose one of these templates down here. In ASP.NET 5, web pages, NVC and web API are consolidated into a unified API. So we no longer have three separate technologies. These things are combined together. Now we don't have anything as far as web forms yet. There has been some conflicting information coming out of Microsoft regarding web forms. And as far as I know, there's not going to be anything new for web forms with the initial release of ASP.NET 5. So for right now, we are limited to MVC, web API, and web pages, if you can call that a limitation. Personally, I would like to see web forms go away. But of course it can't, at least not right now. Well, let's create an empty ASP.NET 5 application. And we will take a look at the folder structure, as well as a few things that are required for an ASP.NET 5 application. Now the first thing that's going to come up is this welcome page. And right now there's really nothing of use here, although there is some information. If you wanted to click on any one of these links then we could do that. But let's close that because what's more interesting is this startup class. Every ASP.NET 5 application needs to have this start up class because, well, this is the start up of our application. This class has a method called configure and it has a parameter called app. It is of type I application builder. And you can see that this method is using this app object to essentially create an application. This is saying that for every request we're going to write this message to the response. So, it doesn't matter what we make a request for, whenever we run this we are always going to see, Hello World. Well I'm going to run this with Ctrl+F5. This is going to run this without debugging. And that's important, because there's a new feature with ASP.NET and Visual Studio, and that is that as our application is running we can actually edit the source code. Go back to the browser and refresh, and then we see the change. We used to not be able to do that. We used to have to stop the application, and then run it again so that everything could recompile. But it's important to note that, right now, that feature is only available if you start without debugging, which is Ctrl+F5. So as you can see, we have this message, Hello World, but if we go back and we can modify this message to just say changed. And if we go back and refresh in the browser, then we can see that did indeed change. We didn't have to restart our application, it was just automatically done. Now of course behind the scenes, it recompiled and all of that stuff, but we didn't have to physically do that. Which is really nice. But, the main takeaway from this portion of the lesson is this configure method. This is where we configure our application. So, whenever we look at the MVC template, we're going to see that the configure method is used to configure a lot of those extra components. Well, let's take a look at the solution explorer and look at the file structure that we have for not only our project, but for the solution itself. Now a solution is a container for one or multiple projects, but they are usually related in some way. For example, we have this web application one, that is the application that we are working on. But if we wanted to break out some of the functionality into a class library, then we could include that class library project in this same solution. So that is the difference between a solution and a project. Now at the top underneath Solution Items, we have this global.json file. And as its name implies, this is global, these are settings for the solution as a whole. You can see that there are two settings. There's one for projects and it has source and test. So evidently there's supposed to be a test project here but it's not listed. But it's not causing any problems because the application works. And then there's the SDK in the version, this sets to beta4 of ASP.NET 5. Now lets close this, and let's go to the references underneath our project. And we're going to see two things, we see DNX 4.5.1 and then we see DNX Core 5.0. Now these are execution contexts. However, it's easier to say, what .NET platform do we want to target? The dnx4.5.1is the full version of the .NET framework, so we can target dnx4.5.1 and we can have at our disposal every class that is available in the .NET framework. However, we can also target the DNS core 5.0. In which case, we wouldn't have the full power of the .NET framework but we would also have a portable application. So if we wanted to write our application here in Windows and then put it onto an Apple Machine or a Linux machine, then we would want to target DNX core 5.0. And if you drill down you're going to see some similarities between these to contexts. For example for DNX 4.5.1 we have Microsoft ASP.NET server IIS and then we also have this web listener. But if we scroll down and go to DNX core 5.0, we have those same things. But you can also see that the DNX 4.5.1 has mscorlib, has System, System.core. And if we wanted to pull in any other .NET references, then we could do that there. And whenever we look at the MVC templates, we're going to see a lot of things listed underneath both of these target platforms. The next folder that we see is wwwroot. This is essentially the folder that we would put static files and any other resources that are going to be sent to the client. So images, JavaScript files, CSS, any other static files, those would inside of wwwroot. And then we have a folder for dependencies. Now these are primarily for client side dependencies and whenever we look at the website template, we're going to see something underneath dependencies, we're going to see bower dependencies as well as NPM. But once again, these are primarily for client-side development. Then there's a project.json file that contains settings for our project. Let's open that up. And we can see a variety of settings for our project, like for example, the frameworks we have dnx4.5.1 and dnxcore50. And if we wanted to include any other dependencies of either of these frameworks, then we would do that here. And so let's go ahead and create another application. But this time we are going to choose the website template. So we once again want an ASP.NET web application, let's just use the default name. And let's choose website and click OK. This is going to give us a lot of extra stuff to see, especially underneath the references, underneath the dependencies, well just about everywhere. Once again, we have this welcome to ASP.NET preview, so let's close that. And let's start with the Startup.cs file, and let's look at the differences here. Now as you can see, by all of the using statements, there's a ton of differences. And if we scroll down, then we see what those differences are but you can see that things are being configured here. We are setting up the configuration. We're adding a json file called config.json. Then there's another json file that we could use called config and then our EnvironmentName and json, and if we scroll on down, we can see a lot of these other services that are being set up. For example, we are adding entity framework here, we are adding SQL server and adding a db context. We're also adding identity. We can also see that Facebook authentication is being configured as well as Microsoft authentication. And then here's a method that adds NVC to our services. If we scroll on down to the configure method, there's a variety of other things going on here. Now of course, one thing to remember is that this is still preview, this is beta. And with the final release of ASP.NET this will probably be configured a little bit differently. I imagine some of these things will be put into separate files. But of course, only time will tell. So let's scroll up in the solution explorer and let's take a look at our references. If we look at DNX 4.5.1, we see a ton of things there. And if we look at dnxcore5.0, we see a ton of things there. And if we were to compare the differences, we would find a lot of similarities. Of course, the main differences are that we have mscorlib, system, system.core, inside of the DNX 4.5.1. Because, once again, that's targeting the full blown version of .NET. Now, inside of the wwwroot folder, we have our CSS files, our images, all of the JavaScript libraries that are being used. And then we can also look at the dependencies, where we see that there are some bower dependencies, as well some NPM. But once again, these are primarily for client-side development. This isn't for anything on the server side. But if you'll notice, all of the code for the application itself, all of the controllers, the views, and all of that is outside of wwwroot. So nothing that is outside of wwwroot is going to be served to the client, unless if it's something like NVC, where the framework is going to take the view and then generate the HTML and send that as a response. But the view files themselves are not sent to the client. And then if we scroll on down to the project.json file, then we can look at the dependencies that are being used within our application, and there are a lot of them. And we can manage our dependencies here inside of this json file, or we could also do that up here with in our references. And so that now you are somewhat familiar with the bones of a ASP.NET 5 application, you know the start up class, and the folder hierarchy. In the next lesson we're going to start writing an application.