Malekbenz

Hi, I'm MalekBenz. I author this blog, I'm FullStack Developer, create courses & love learning, writing, teaching technology. ( Javascript, C#, ASP.NET , NodeJS, SQL Server )

Build your first ASP.NET Core application on linux

ASP.NET Core is an open source web framework for building modern web applications that can be developed and run on Windows, Linux and the Mac. It includes the MVC framework, which now combines the features of MVC and Web API into a single web programming framework. ASP.NET Core is built on the .NET Core runtime, but it can also be run on the full .NET Framework for maximum compatibility. ASP.NET Core is a significant redesign of ASP.NET. This post introduces you how create your first ASP.NET Core application.

In order to install .NET Core on Ubuntu or Linux Mint you can see Install .Net Core on linux.

Create .Net Core project

Let’s Create HelloASP application


$ mkdir helloASP
$ cd hellodASP
$ dotnet new

CMD

Add the Kestrel package

Update the project.json file to add the Kestrel HTTP server package as a dependency:

    {
    "version": "1.0.0-*",
    "buildOptions": {
        "debugType": "portable",
        "emitEntryPoint": true
    },
    "dependencies": {},
    "frameworks": {
        "netcoreapp1.0": {
        "dependencies": {
            "Microsoft.NETCore.App": {
            "type": "platform",
            "version": "1.0.0"
            },
            "Microsoft.AspNetCore.Server.Kestrel": "1.0.0" 
        },
        "imports": "dnxcore50"
        }
    }
    }

CMD

And then restore the packages:

$ dotnet restore

Startup.cs file:

Add a Startup.cs file that defines the request handling logic:

    using System;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;

    namespace aspnetcoreapp
    {
        public class Startup
        {
            public void Configure(IApplicationBuilder app)
            {
                app.Run(context =>
                {
                    return context.Response.WriteAsync("Hello from ASP.NET Core!");
                });
            }
        }
    }

CMD

Update Program.cs:

Update the code in Program.cs to setup and start the Web host:

using System;
using Microsoft.AspNetCore.Hosting;

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

            host.Run();
        }
    }
}

CMD

Run the app

$ dotnet run

CMD

Browse to http://localhost:5000

CMD

Congratulations!

Comments