Reynald guides the student through setting up the core plumbing necessary for the ASP.NET WEB application. He sets up the project, creates a model, controller, and the data context. Then, he shows how Visual Studio starts gathering all the necessary information such as third-party software for the application generating the code that's needed to build the views to create, update, and delete by scaffolding.
- [Instructor] We're going to go ahead and create an ASP.NET application that incorporates MVC. And currently I have Visual Studio Community 2015, which we're going to be using. Right now, Visual Studio 2015 is updated up to three. If you go to help, about Microsoft Visual Studio, which is the the last option, choose that and you can check the version. You can actually check this for most applications on Windows.
To check the version just go into help and about. So update three. Now the other thing that we'll want to make sure to have installed is the tooling. Visual Studio 2015 Tooling Preview 2 is what's available as of the date of this recording. And you can get both the Visual Studio Community edition with the update and the tooling from microsoft.com/net/core. Alright, once those are set up, you can go ahead and get started.
Let's go to our file menu and choose new project. And what you'll want to make sure is on the lefthand side under templates, you'll see an option for Visual C# and then under that, Windows and then Web. We'll want to make sure Web is highlighted and then we'll get these three options to determine what type of project we're going to set up. And what we want is the first one, ASP.NET Web Application for the .NET framework. We're not going to be dealing with the .NET Core in this video right here.
Let's go ahead and choose that and then on the righthand side you'll see there's an option for add application insights. We don't want to mess with that. Let's go ahead and uncheck that. And then, name our solution. Let's give it a name of StudentDb, for student database. We're creating a student database and we're going to want it to have CRUD operations for creating records, reading records, updating records, and deleting it. Let's go ahead and make sure we choose MVC.
That is going to be the design pattern that we're going to be adhering to. And confirm that MVC is checked at the bottom, which is should be if we selected it at the top. And make sure that host in the cloud in the bottom righthand corner is unchecked. And also for change authentication right above it, select that and make sure that we choose no authentication. We are keeping this very simple for this example. We'll click okay. And then okay once again to get Visual Studio ready to start preparing all the files that it needs in order to set this project up.
Okay, looks like Visual did all the hard work setting up basic plumbing. If we look on the righthand side, we can see we have our solution with our properties and references we can look at. But what's more outstanding for this particular project is that it has a folder for the controllers and our models, which is currently empty, and then the views. That's the core focus of the MVC model. Let's build and run, just to make sure that it at least starts up and everything's okay before we do any further work.
I'll go ahead and execute the program. And there you go, right out of the box a basic web application with a couple of tabs, one for home, one for about, one for contact. And you can come in and modify. Okay, looks good so far. Let's go ahead and close this window. I'll stop debugging right here. Now let's actually start off with our model. Let's go ahead and create a model class that's going to have the properties for this student data that we're going to be referencing.
I'll right-click on models, choose add class, and we'll call this Student. Make sure class is selected. And click add. And within here I'll add three simple properties. One for student ID, one for name, and one for year. For setting up properties, I like to do a little shortcut by typing in prop and then hitting the tab key twice, and Visual Studio drops in a stub so that I can overwrite.
And the first property is going to be a type integer, so I'll skip that by hitting tab again, and then enter in StudentId. And right at this point, I can go ahead and hit enter. It brings the cursor to the end of the line and then hit it again and it'll go to the second line. Now I'll do the same, except this time we want the name property, which is going to be a string. So I'll hit string, str. Str just brings up the IntelliSense to string.
The once you see it, you can hit tab. And now, I'll enter a name and hit enter, enter again. And now we'll do our last property, which will be for year. This will also be a string because the year for the student is going to be a word, freshman, sophomore, junior, that nature. Not a numerical year. I'll enter in year and control+s to save. And we are good there. So now I will go to compile and we're done with setting up our first model.
The next thing we want to do is to create a controller. I'll right-click on the controller folder and go down to add and choose controller. And what I want to choose here is MVC 5 Controller with views, using Entity Framework. That's our third option here, MVC 5 Controller with views, using Entity Framework. And what that's going to do is allow Visual Studio to generate a lot of code to save us time on the backend to create the views associated with the controller and it's going to leverage Entity Framework as our ORM, object relational mapper, so that we can do our transactions with our database.
When the add controller dialog pops up, we'll want to make sure we choose a model class of StudentDb, which is what we just created. And then confirm that the controller name is StudentsController, which it is. And make sure that Use async controller actions is unchecked. We have the capability to check this so that when it generates code it can set up the code so it uses the async features that .NET offers.
But we're not going to use it in this case. And let's go ahead and choose the plus sign here. And we're going to name our context StudentDb.Models.StudentDbContext. And that's the default, so we're going to leave it as that. And click add. And that should be it. We go ahead and click add one more time. And now Visual Studio starts gathering all the necessary information, such as third party software for the application generating the code that's needed to build the views to create, update and delete.
That's what it's been doing right now in the background while it's scaffolding. Okay, now it looks like we have the code that it generated. You can go ahead and actually take some time to observe the code that it set up here. This is in the controller. You can see it has a method for editing. If I go to the upper righthand corner, we get a good glance at the different methods that it's set up. But there's some for creating, and deleting, editing, and basically viewing, which is equivalent to reading, which is on our index.
That's like the very first method that you see here, in ActionResult with Index. Now if you're familiar with MVC, these methods will seem very familiar to you. If not, you don't need to be too concerned about it right now. But the main purpose is to get familiar with how we can use this framework to create a basic application using the .NET libraries.
- Understanding CLR and FCL
- Working with the .NET compiler
- Setting up Visual Studio to code with ASP.NET Core
- Creating an ASP.NET MVC 6 app
- Creating a web API
- Working with C# features
- Testing in C#
- Deploying cross-platform .NET apps