One of the biggest things to come from Microsoft recently, for developers, is .NET core, which is a lightweight, multi-platform framework for creating web services on Windows, Linux, and Mac. And not just multi-platform, but cross-platform as well. So we can create universal apps that target all 3 of the major platforms currently. Which means more collaboration and less restrictions overall when developing new projects.

How It Works

ASP.NET Core apps run on either .NET Core or on the .NET Framework. It consists of modular components with minimal overhead, so you retain flexibility while constructing your solutions. ASP.NET Core is no longer based on System.Web.dll. It is based on a set of granular and well factored NuGet packages. And it's important to note, that it is a completely new framework, hence the 1.0, and not a replacement for ASP.NET 4.6. So functionality like Web Pages are still missing, but in time ASP.NET Core will become the primary framework.

Installing .NET Core

The first thing you'll want to do is install .NET core on your machine, which you can grab from here. I'm targeting Windows on this one, but obviously you're free to go with whichever platform you'd like. The installation is pretty straight forward and just takes a minute. If you're going to be using Visual Studio 2015 for your coding, you'll want to get Visual Studio Update 3 and .NET Core for Visual Studio, both of which are in that link I just mentioned. If you're going to be working on just the command line however, or with Visual Studio Code, you only need to install the .NET Core SDK for Windows.

Quick Test

Now that that's up and running, you can test out if .NET Core installed correctly in the command line. In your command line, head on over to the root with the following:

cd c:/

Let's create a new directory:

mkdir aspnetcore
cd aspnetcore

Now let's create a new .NET Core project with the following:

dotnet new
Taking A Look At ASP.NET Core

And now we can run this application, which is essentially a 'hello world' app.

dotnet restore
dotnet run

We have to restore the application first before it will successfully run. This essentially restores the packages that are listed in the project.json file that's in the project.

Taking A Look At ASP.NET Core
Taking A Look At ASP.NET Core

And if we take a look at the project directory, we will see that all of the appropriate files are sitting right there.

Taking A Look At ASP.NET Core

Add HTTP Server

We've created a .NET Core project. Now let's turn that into an ASP.NET Core project. We're going to need to add a web server for this, and ASP.NET Core is essentially a console application that instantiates a web server on startup. In our case, that would be the Kestrel HTTP Server.

Startup.cs

You have total control of how requests are handled in your app, and that is done through the Services class that we have to define. The Startup class sets up services and the configuration that you will use. Here's a quick example of how that's going to look:


using System;
using Microsoft.AspNetCore.Hosting;

namespace aspnetcoreapp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseStartup()
                .Build();

            host.Run();
        }
    }
}

You can add this file to the main root of your project and be sure that the main program file and this services.cs file both have a matching namespace, or you will receive an error when the Services class isn't found.

Lastly, we can now update our Program.cs file and our main method to use the web server and to run it. And that should look something like the following.


using System;
using Microsoft.AspNetCore.Hosting;

namespace aspnetcoreapp
{
    public class Program
    {
        public static void Main(string[] args)
        {
                var host = new WebHostBuilder()
                .UseKestrel()
                .UseStartup()
                .Build();

                host.Run();
        }
    }
}

Now we can do another dotnet restore and a dotnet run and you should get the following success message, along with the port number of where the server is listening.

Taking A Look At ASP.NET Core
Taking A Look At ASP.NET Core

And that, is just a small part of ASP.NET Core. But you can see how it functions in a more loose manner than previous versions of ASP.NET. And the command line tools are much more intuitive and less cumbersome than before.

Few Last Things

I just showed a quick console application that spits out a string back to the browser. Most websites of course do not function that way. As least, I would hope they wouldn't. But ASP.NET Core supports both Web API and MVC projects, which you can build in Visual Studio 2015, Visual Studio Code or whichever text editor you prefer really. And this isn't a replacement for previous robust and stable versions of ASP.NET. If you have your business solutions running in ASP.NET 4.6 then it probably wouldn't be a great idea to start converting your code to Core. But if you're working on a new application, then for sure making use of Core's lightweight and cross-platform features might be the way to go.

Walter G. is a software engineer, startup co-founder, former CTO of several tech companies and currently teaches programming for a coding bootcamp. He has been blogging for the past 5 years and is an avid BMX rider, bio-hacker and performance enthusiast.
If you read this far, then I hope you enjoyed this post and found it useful! Consider adding to my daily coffee funds to continue to provide better and more helpful articles in the future!
Maybe later

Add a comment

Send me your weekly newsletter filled with awesome ideas
Post comment