The Minimal ASPNET Core App

The Minimal ASPNET Core App

Now updated for ASP.NET Core 1.1 here.

There’s no upper limit on how large an ASP.NET Core app can get, but what about how small you can make it? With the new .NET Core framework, the answer is pretty small. Let’s see just how small.

First, make sure you have the dotnet CLI tools installed. You can get them from dot.net. This sample will work on Windows as well as Mac and Unix.

Next, open a command prompt, create a new folder (e.g. ‘min’) and run ‘dotnet new’ to create a simple console application. This will produce two files:

  • Program.cs
  • project.json

Now, at this point you can run the application by running ‘dotnet restore’ and ‘dotnet run’ and you’ll get the standard Hello World output. But that’s not an ASP.NET Core app (yet).

To make this console application an ASP.NET Core app, you need to add “Microsoft.AspNetCore.Server.Kestrel”:”1.0.0″ to the “dependencies” property in project.json. When you’re done, it should look like this:

Now, open Program.cs. Normally your ASP.NET apps will have separate files for Startup.cs, Program.cs, and probably MVC-related folders and files for controllers, views, etc. For this example, though, let’s show how you can set it all up in just Program.cs. Program.cs is the entry point for the application. Instead of having it print “Hello World” we’re going to configure a web host (using a WebHostBuilder), tell it to use the Kestrel server, and set it up to respond to all requests by simply writing a reponse of “Hi!”. Here’s the end result:

From your command prompt, type ‘dotnet run’ and you should see the app start up on localhost:5000 by default. Browse to that URL and you should see “Hi!”. That’s about as small as I can get a working ASP.NET Core app while still making it readable.

  • Prasid Joshi

    Its a great post Steve. I see that we need to resolve dependencies by running ‘dotnet restore’, every time we add new dependencies to project.json file. Is there some way that it resolves itself while we run ‘dotnet run’. One simple solution may be to create an alias of the command.

    • ardalis

      The three main commands are dotnet restore, dotnet build, and dotnet run. The first time you create a new project, you need to run restore manually. After that, you can typically just use run. Run will automatically call build if required.

  • I like it very much.

  • David Fowler

    You can get rid of the imports

  • John Madison

    I wanted a better understanding of what classes where at play so I un-chained the code.
    It couldn’t find reference to the methods on MSDN for some reason. So it took a lot of trial and
    error to find the correct types and imports. If anyone could recommend how to find import information easier next time, it would be greatly appreciated. Always seems to be more of a hassle than it should be.

    Microsoft.AspNetCore.Hosting.WebHostBuilder w1;
    Microsoft.AspNetCore.Hosting.IWebHostBuilder w2;
    Microsoft.AspNetCore.Hosting.IWebHostBuilder w3;
    Microsoft.AspNetCore.Hosting.IWebHost w4;

    w1 = new WebHostBuilder();
    w2 = w1.UseKestrel();
    w3 = w2.Configure(
    a => a.Run(c => c.Response.WriteAsync(“[WHAT IS UP]”))
    );

    w4 = w3.Build();
    w4.Run();