ASP.NET Core hello World
In this post, we’ll create an ASP.NET Core app that contains a page containing “hello world” and then change the app to use the ASP.NET welcome page …
Create an empty web project
When you create a new project, select ASP.NET Web Application from the list of templates. Don’t worry about the framework version on this dialog.
In this example we are then going to select ASP.NET Core Preview Empty from the list of ASP.NET templates.
Configure startup
The entry point for an ASP.NET Core app happens in Startup.cs.
The ConfigureServices method is used to set up the services that the app needs to use - as the name suggests!
The Configure method is used to define what happens during the request pipeline. We can use IApplicationBuilder.Run() in order to do a very simple hello world.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app)
{
app.Run(async context =>
{
await context.Response.WriteAsync("Hello World");
});
}
}
If you run the project, you should receive “Hello World” on a web page.
Using a welcome page
We can use the standard ASP.NET welcome page instead of our “Hello World” page by instructing the request pipeline to do so:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app)
{
app.UseWelcomePage();
}
}
For this to compile, you’ll need to add a dependency on Microsoft.AspNet.Diagnostics
Now, when you run the project, you should receive the ASP.NET welcome page.
If you to learn about using React with ASP.NET Core you might find my book useful: