Here are simple tutorial how to build your ASP.NET Core application. It’s just simple steps and you can follow it easily.
Install .NET Core First
First, please make sure you install .NET Core SDK for Windows
With that installed, it’s time to build your first application.
Open up a command prompt (or use the in-built terminal in Visual Studio Code) and start by creating a folder for your application, then initialising it.
mkdir CoreApp
cd CoreApp
dotnet new
When you run the dotnet command for the first time you’ll see some information about how .NET Core collects usage data and initially populates a local package cache. Once you’ve seen this message you won’t see it again on the same machine.
Project.json and Program.cs File
One of the most striking things about getting started with .NET Core is that you don’t end up with lots of files and dependencies right out of the gate, just the minimum you need for an empty web site.
In fact, all you’ll be left with is a project.json and Program.cs file.
At this point, this is just a .net Core app (not web) which prints “Hello World” to the console.
If you take a look at project.json you’ll see the minimum dependencies your new .net core app needs to run.
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
}
}
}
When you create a new .net core application like this, it’s dependencies are not downloaded automatically. To do that, you simply need to issue a new command to restore the dependencies and then to run the app.
dotnet restore
dotnet run
Launch the Application
So your app just compiled and launched. It looks suspiciously like a console application, because it is a console application.
Up until now, everything you’ve done has resulted in a minimal .net core console application.
So how do you turn it into a web site?
Well you need to update project.json to tell it to add the Kestrel HTTP server as a dependency.
Kestrel is a lightning fast cross-platform web server which can be used to self-host your web application. In other words, you can tell your new web app that Kestrel is a dependency and then run your site on it without relying on IIS or IIS Express.
{
"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"
}
}
}
You’ve added a dependency so now you need to download that dependency using the restore command.
dotnet restore
Incidentally, in case you’re wondering where donet restore is restoring packages to, the default location is %userprofile%\.nuget\packages
Next up, you’ll need to create a Startup.cs file. This will define how incoming web requests should be handled.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
namespace ConsoleApplication {
public class Startup{
public void Configure(IApplicationBuilder app){
app.Run(context => {
return context.Response.WriteAsync("Hello world");
});
}
}
}
This is about as simple as it comes, you simply tell your app to always return a response and write the text “Hello world” to it (for any request to your web application).
At this point, you haven’t told your app to start Kestrel (to start accepting web requests). You can do that by updating Program.cs.
using System;
using Microsoft.AspNetCore.Hosting;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var host = new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
Should you wish, you can happily delete Console.WriteLine("Hello World!") as you’ve replaced it with something far more useful, you’ve told Core to launch Kestrel using the Startup class you just created.
Go ahead and run your app.
dotnet run
You’ll see a message telling you that your app is up and running and where you can access it.
Hosting environment: Development
Content root path: C:\Users\Jervis\CoreApp\bin\Debug\netcoreapp1.0
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Hit http://localhost:5000 in a browser!