What is the use of the Configure() method of startup class in C# Asp.net Core?

CsharpServer Side ProgrammingProgramming

The configure method is present inside startup class of ASP.NET Core application

The Configure method is a place where you can configure application request pipeline for your application using IApplicationBuilder instance that is provided by the built-in IoC container

The Configure method by default has these three parameters IApplicationBuilder, IWebHostEnvironment and ILoggerFactory .

At run time, the ConfigureServices method is called before the Configure method. This is to register custom service with the IoC container which can be used in Configure method.

IWebHostEnvironment :Provides information about the web hosting environment an application is running in.

IApplicationBuilder:Defines a class that provides the mechanisms to configure an application's request pipeline.

Example

public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
   if (env.IsDevelopment()){
      app.UseDeveloperExceptionPage();
   } else {
      app.UseExceptionHandler("/Error");
      app.UseHsts();
   }
   app.UseHttpsRedirection();
   app.UseStaticFiles();
   app.UseRouting();
   app.UseAuthorization();
   app.UseEndpoints(endpoints =>{
      endpoints.MapRazorPages();
   });
}
raja
Published on 24-Sep-2020 16:36:08
Advertisements