Web
Analytics
ASP.NET Core 2 application project structure | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

ASP.NET Core 2 application project structure

After the creation of empty asp.net core project you have seen  that empty project is not completely empty, and does, in fact, include a couple of files:

  • Program.cs: This is the entry point of the application. It loads configuration, initiates the logging framework, and more. This file is mostly used to configure web server related configuration.
  • Startup.cs: This is run when the web project starts. It configures the application services and requests response pipeline components.
  • The wwwroot folder: This folder is where all of the static assets of the application will be stored for example html pages, images, css, js files etc.




Configure Service Method

Configure service method Declaration is not compulsory in startup class. This method is used to configure services that are used by the application. When the application is requested for the first time, it calls ConfigureServices method. This method must be declared with a public access modifier so that the environment will be able to read the content from metadata.

ASP.net core has built-in support for Dependency Injection. We can add services to DI container using this method. Following are ways to define ConfigureServices method in startup class.

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

services.AddMvc() method is used to inject and configure the MVC design pattern so that we can work as a traditional asp.net MVC 5 based application. Using of configuring service method we can add custom classes as a dependency.

Configure Method

This method is used to define how the application will respond on each HTTP request i.e. we can control the ASP.net pipeline. This method is also used to configure middleware in HTTP pipeline. This method accepts IApplicationBuilder as a parameter. This method may accept some optional parameter such as IHostingEnvironment and ILoggerFactory. Whenever any service is added to ConfigureServices method, it is available to use in this method.

This method is used to add middlewares for exception handling, convention routing configuration, static files configuration, etc.

Program code execution order:

  • First Program.cs file is executed by the compiler which is responsible for web server configuration and also designed to call startup.cs class.
  • The Startup class is executed only once during an application’s lifespan, right after the web server has loaded the ASP.NET Core application.
  • The following screenshot shows the order of execution of the different methods in the classes involved in ASP.NET Core initialization:

asp.net core 2 application folder structure