Core MVC applications use Kestrel, an open source, cross platform web server based on Libuv. This enables you to run Core MVC applications three different ways—from Visual Studio using IIS and Kestrel, from Visual Studio using just Kestrel, and from the .NET Core CLI using Kestrel. Learn about the launchsettings.json file that controls how the application is run from Visual Studio.
- Before we go any further, let's talk about how you run a core MVC Application. And to understand that, we have to understand what a core MVC Application is. It's really just a console application that creates a web server. The WebHostBuilder, which is called in the submain, creates a web server and it can be either IIS or Kestrel. Kestrel is a cross-platform web server based on libuv, and if you want to learn more about Kestrel and performance considerations, look at the URL on your screen.
There are three ways to run an MVC Application. At a Visual Studio, you can run it using Kestrel or IIS. And from the command line, that is the dotnet core command line interface, you use a dotnet-run command and that runs the application in Kestrel. To run your application from Visual Studio using IIS Express, you just click on the run button here. There's also a profile called Core MVC Movie, and this will run it in Kestrel.
To configure these profiles, you go to the properties node and open up launchSettings.json. The IIS settings will launch the URL using port 10110, and your port will vary. Visual Studio, when it creates a project, assigns random port numbers and Kestrel will use port 10111. Now at the time of this recording, there is an issue with Browser Link so I need to go in and uninstall Browser Link.
Hopefully by the time you're watching this this has been resolved. I'm going to go ahead and just uninstall it. So remove the package. Need to confirm that. I accept. One other change we need to make and that is go into the Startup Class. Don't worry. We're talk more about this class later. I'm going to comment out this line that says Use Browser Link. So now when I click run with IIS, it'll fire up the homepage using that 10110 port.
There we have our very simple application running. We close the browser. Stop the app. Then it will switch us over to Kestrel and hit run. We see a browser with the 10111 port and what you don't see off-screen, I'm going to pull it over onto this screen, is it also fires up a dotnet command window. Well it's a standard command window running dotnet and shows you everything going on.
If I scroll to the very top, right here it says Kestrel's now listening on port 10111. I can hit Control + C to shutdown. If I hit Control + C in this window, it shuts down Kestrel. I can also hit stop in Visual Studio as well. The other way to run it is from the command line. So I have a command window open in the location where my Csproj file is located and I just type dotnet run.
This will fire up Kestrel from the command line, but you see it's using port 5000 and did not launch a browser. So let's fix the port and we do that by going into the Program. We'll hide the trace points. In the main method, the entry point, this is where it's building the web host. Dotnet Core is very much about an opt-in mentality instead of having to opt-out.
Right out of the gate, the web host builder gives you nothing. The default template turns on Kestrel IS Integration, sets the content route to the current directory, sets the startup class to a class named Startup, turns on Application Insights which is a way of monitoring application, builds all of these options together, and then runs the web server. What we want to do is tell Kestrel to use a different port.
So what we do is we add in star colon and we're going to go back to the 10111 port. This will only be called from the command line. So now if I run from the command line again and type in dotnet run, we see it is now using port 10111. So those are the three ways that you can run a core MVC Application.
Phil Japikse begins by showing how to install and update the .NET Core SDK. He reviews the functionality of the MVC 5 app, explains how to create necessary projects, and discusses migrating static content. Next, he demonstrates how to create a data access layer, complete the server-side migration, set up the HTTP pipeline, add custom items into the dependency injection container, leverage the new project configuration system, and migrate the controllers. Phil then introduces Tag Helpers—one the big new features in ASP.NET Core—and uses them to migrate and simplify the views. Phil also demonstrates how to create and use custom Tag Helpers. To wrap up, he covers working with view components, explaining what they are and why they're helpful. He walks through how to create the server-side view component code, and how to refactor your app and invoke the view component.
- Reviewing the MVC 5 application
- Creating the data access layer
- Adding and updating the models
- Updating the database
- Completing the server-side migration
- Configuring the HTTP pipeline
- Configuring and using dependency injection
- Migrating the views
- Creating view components