Basic programming, .NET technology.

1 - [Updated] - ASP .Net core - Fundamentals - Startup

 This series is a collection of knowledge about ASP .NET Core. It just is my notes.

Part 2: Dependency Injection

Part 3: Middleware.

Part 4: Host and Servers.

Part 5: Configurations

Part 6: Environment.

Part 7: Logs.

Part 8: Error Handling.

Part 9: Routing.

Part 10: Make an HTTP Request.

Part 11: Static files.

Part 12: Authentication and Authorization.

Part 13: CORS.

I take some notes for the startup file in Asp .net core:

  • Specified when app host is built.
  • ConfigureServices method:
    • called by the host before Configure method
    • Use to set up services for app
    • There is some build-in extension method (AddDbContext, AddEntityFrameworkStores..)
    • Usually used to add business services/ DAL class to dependency injection.
      • You can write an extension method on IServiceCollection to add more services as you want.
  • Configure method
    • used to specify how the app responds to an HTTP request.
    • to configure adding  middleware component to IApplicationBuilder
    • there are some extension methods to  add middleware
    • You can write a new extension method by using IApplicationBuilder  (Ex: write custom middleware)
    • How to write custom middleware
  • Multiple startups
    • for different environments.
    • suffix name matched with the current environment is prioritized, for example (StartupDevelopment)
  • [Updated] - ASP.NET Core 6+
Startup code in the program file because of the minimal hosting model.

  var builder = WebApplication.CreateBuilder(args);
    // Add services to the container.
    builder.Services.AddRazorPages();
    builder.Services.AddControllersWithViews();
    // You can add your user-defined services here
    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    // You can add your custom middle ware here
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    ...
    app.Run();


References:

[Microsoft Learn] - App startup in ASP.NET Core

    Share:

    0 nhận xét:

    Đăng nhận xét

    Featured Posts

    Data type 3 - string type