Creating an ASP.NET core web application

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:

  1. Open Visual Studio and click on the menu item by navigating to File | New | Project.
  2. Navigate to Visual C# from the installed template and select Web.

 

  1. Then, select ASP.NET Core Web Application and enter the application name as My Todo, as shown in the following screenshot:
Creating a project named My Todo
  1. Select the ASP.NET Core Empty template and click on Ok to create the project, as illustrated:
Select an empty ASP.NET Core template

The solution structure of the My Todo application that we created is shown in the following screenshot:

The default solution structure of My Todo

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:

The default code of the Startup class

So, when you run the application, you will get the following output in the browser:

The default output of the 'My Todo' project

Now, let's make the application to serve a default page for any request coming through. Follow these steps to do so:

  1. Select the wwwroot folder under the My Todo project. Right-click on selecting the project and navigate to Add and click New Item:
Navigate to the Add New Item menu
  1. 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:
Name the HTML file as index.html
  1. Update the content of the index.html file, as follows:
The updated code of index.html
  1. 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!");
});
  1. Add the following code to the Configure method so that the pipeline serves default and static files to the request:
The code to enable the pipeline to serve static and default files
  1. You need to add the Microsoft.AspNetCore.StaticFiles NuGet package, as shown, in order to use these extensions:
Adding a reference to a namespace if required
  1. 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:
The output of the application after adding index.html
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.251.154