Working with the Startup class

Another autogenerated element, which exists in all types of ASP.NET Core 3 projects, is the Startup class. As you have seen previously, the Program class mainly handles everything associated with the hosting environment. The Startup class is all about the preloading and configuration of your services and middleware. Those two classes are the foundations of all ASP.NET Core 3 applications.

Let's now look at the basic structure of the Startup class to get a better understanding of what is provided and how to make the best use of its functionalities:

 

      public class Startup 
      { 
        public void ConfigureServices(IServiceCollection services)  {   } 
 
        public void Configure(IApplicationBuilder app,   
IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
}); }

There are two methods that will require your attention since you will be working with them on a fairly regular basis:

  • The ConfigureServices method, called by the runtime and used to add services to the container
  • The Configure method used to configure the HTTP pipeline

We said at the beginning of the chapter that we wanted more practical work, so let's get back to our Tic-Tac-Toe game and see how to use the Startup class in a real example!

We are going to use MVC for implementing the application, but, since you have used the empty ASP.NET Core 3.0 Web Application template, nothing has been added by Visual Studio 2019 during project generation. You have to add everything by yourself; what a wonderful opportunity for a better understanding of how everything works!

The first thing to do is to add MVC to the services configuration. You do that by using the ConfigureServices method and just adding the MVC Middleware:

    public void ConfigureServices(IServiceCollection services) 
    { 
      services.AddControllersWithViews();
    } 

You might say that this was too easy; so, what's the catch? There is no catch! Everything in ASP.NET Core 3 was developed around simplicity, clarity, and developer productivity.

You can see this again when configuring your MVC Middleware and setting the routing path (we will explain routing in more detail later):

             app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});

Again, we have here very clear and short instructions that make our lives as developers easier and more productive. Now is a really good time to be a developer!

In the next step, you need to enable the use of static content within your ASP.NET Core 3 application, in order to use HTML, CSS, JavaScript, and images.

Do you know how to do that? Yes, you are right; you need to add another middleware. You do that just like before by calling the corresponding app method:

    app.UseStaticFiles(); 

The following is an example of a Startup.cs class you could use for the Tic-Tac-Toe game after having configured the various service settings seen previously:

    public class Startup                                       { 
      public void ConfigureServices(IServiceCollection services) 
      { services.AddControllersWithViews();  } 
 
      public void Configure(IApplicationBuilder app, 
IHostingEnvironment env){ if (env.IsDevelopment()) app.UseDeveloperExceptionPage(); else app.UseExceptionHandler("/Home/Error"); app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
}); }

Please note that there are many other services that you can add to the ConfigureServices method, examples of which include services.AddAuthorization(), or services.AddAspnetCoreIdentity(), already provided for by the ASP.NET Core Framework, and which, indeed, could be created on your own. Whatever is configured here is accessible throughout your entire application through dependency injection (DI), and we have dedicated a section on this in Chapter 5, Basic Concepts of ASP.NET Core 3 via a Custom Application: Part 2.

Now that we have an idea of the classes that are there by default as start up classes for an ASP.NET Core application, now is a good time to look at what a basic project structure will look like, as discussed in the following section. 

..................Content has been hidden....................

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