creating a .net core aspnet website

In the previous post, we created a .net core console app. In this post we'll take it one step forward and create a simple asp.net website.

You can follow the same steps as before that you used to create the console app.

mkdir helloaspnet  
dotnet new helloaspnet  
cd helloaspnet  

You will then need to modify/create 3 files

1) Program.cs (modify)
2) Startup.cs (create)
3) project.json (modify)

Program.cs
using System;  
using System.Collections.Generic;  
using System.IO;  
using System.Linq;  
using System.Threading.Tasks;  
using Microsoft.AspNetCore.Hosting;  
using Microsoft.AspNetCore.Builder;  
using Microsoft.Extensions.Configuration;

namespace helloaspnet  
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Get environment variables
            var config = new ConfigurationBuilder()
                .AddEnvironmentVariables("")
                .Build();

            // You need to add these lines for accessing outside of Docker
            var url = config["ASPNETCORE_URLS"] ?? "http://*:8080";
            var env = config["ASPNETCORE_ENVIRONMENT"] ?? "Development";

            var host = new WebHostBuilder()
                .UseKestrel()
                .UseUrls(url)
                .UseEnvironment(env)
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

there are a few key points that are of most interest..

1) The use of the kestrel web server

var host = new WebHostBuilder()  
                .UseKestrel()
                .UseUrls(url)
                .UseEnvironment(env)
                .UseStartup<Startup>()
                .Build();

2) The exposition of port 8080

var url = config["ASPNETCORE_URLS"] ?? "http://*:8080";  
Startup.cs

We're not gonna create a terribly complicated application (or even an MVC website), just one that returns some text for any incoming requests.

So the following code needs to be added to Startup.cs

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

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

The project json will need to be updated to include the dependency on the kestrel web server.

"dependencies": {
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
  }

The full file is shown below

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

And now we have a working asp.net website. We can now build and run it...

dotnet restore  
dotnet build  
dotnet run  

And you can either browse to http://localhost:8080 or curl it

and you will get the following response

Hello from ASP.NET Core!  

chris hay

Read more posts by this author.