Let's start this chapter by creating an ASP.NET Core web application. I am assuming that you have Visual Studio 2017 or a later version installed in your development environment. Follow these steps to create the application:
- Open Visual Studio and click on the menu item by navigating to File | New | Project.
- Navigate to Visual C# from the installed template and select Web.
- Then, select ASP.NET Core Web Application and enter the application name as My Todo, as shown in the following screenshot:
- Select the ASP.NET Core Empty template and click on Ok to create the project, as illustrated:
The solution structure of the My Todo application that we created is shown in the following screenshot:
The Startup class is the entry point of an ASP.NET Core web application. The Configure method in the Startup class is used to set up a request pipeline to handle all the requests coming to the application. Here, the default code of the Startup class is configured to return the Hello World! text by default:
So, when you run the application, you will get the following output in the browser:
Now, let's make the application to serve a default page for any request coming through. Follow these steps to do so:
- Select the wwwroot folder under the My Todo project. Right-click on selecting the project and navigate to Add and click New Item:
- In the Add New Item window, click on Content under Web, and then select HTML Page from the center pane. Enter index.html as the filename and click on Add:
- Update the content of the index.html file, as follows:
- Open the Startup class and delete the following code snippet that writes the Hello World default text to a response for each request:
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello
World!");
});
- Add the following code to the Configure method so that the pipeline serves default and static files to the request:
- You need to add the Microsoft.AspNetCore.StaticFiles NuGet package, as shown, in order to use these extensions:
- Now, add an index.html file under the wwwroot folder and run the application by pressing F5. You will note that the application serves the index.html file as the default file for the request. Here, I have added an h1 tag with the content My Todo Landing Page: