ASP.NET CORE MVC - Program.cs

 

Program.cs

This code is the entry point of an ASP.NET Core MVC application. It configures the application pipeline, sets up services, and defines how HTTP requests are handled. Let’s break it down line by line:

1. public static void Main(string[] args)

  • This is the entry point of the application.

  • The Main method is called when the application starts.

  • args is an array of command-line arguments passed to the application.


2. var builder = WebApplication.CreateBuilder(args);

  • Creates a WebApplicationBuilder object, which is used to configure the application.

  • The args parameter allows command-line arguments to be passed to the application for configuration.


3. builder.Services.AddControllersWithViews();

  • Adds services to the dependency injection (DI) container for MVC controllers and views.

  • This enables the application to use the Model-View-Controller (MVC) pattern.


4. var app = builder.Build();

  • Builds the WebApplication object, which represents the application pipeline.

  • This is where the middleware pipeline is configured.


5. if (!app.Environment.IsDevelopment())

  • Checks if the application is running in a non-development environment (e.g., Production, Staging).

  • This is useful for applying environment-specific configurations.


6. app.UseExceptionHandler("/Home/Error");

  • Configures a global exception handler for non-development environments.

  • If an unhandled exception occurs, the user is redirected to the /Home/Error route.


7. app.UseHsts();

  • Enables HTTP Strict Transport Security (HSTS).

  • HSTS forces the browser to use HTTPS for all requests, improving security.

  • The default HSTS duration is 30 days, but this can be customized for production.


8. app.UseHttpsRedirection();

  • Redirects all HTTP requests to HTTPS.

  • This ensures that the application uses secure connections.


9. app.UseStaticFiles();

  • Enables serving static files (e.g., CSS, JavaScript, images) from the wwwroot folder.

  • Static files are served directly to the client without being processed by the MVC pipeline.


10. app.UseRouting();

  • Enables routing in the application.

  • Routing maps incoming HTTP requests to the appropriate controller and action.


11. app.UseAuthorization();

  • Enables authorization middleware.

  • This ensures that users have the necessary permissions to access certain resources.


12. app.MapControllerRoute(...);

  • Configures the default route for MVC controllers.

  • The route template is "{controller=Home}/{action=Index}/{id?}", which means:

    • controller=Home: The default controller is HomeController.

    • action=Index: The default action is Index.

    • id?: The id parameter is optional.


13. app.Run();

  • Starts the application and begins listening for incoming HTTP requests.

  • This is the final step in setting up the application pipeline.


public static void Main(string[] args)
{
    // Step 1: Create a WebApplicationBuilder to configure the application.
    var builder = WebApplication.CreateBuilder(args);

    // Step 2: Add MVC services to the dependency injection container.
    builder.Services.AddControllersWithViews();

    // Step 3: Build the WebApplication object.
    var app = builder.Build();

    // Step 4: Configure the HTTP request pipeline based on the environment.
    if (!app.Environment.IsDevelopment())
    {
        // Step 5: Use a global exception handler for non-development environments.
        app.UseExceptionHandler("/Home/Error");

        // Step 6: Enable HTTP Strict Transport Security (HSTS) for secure connections.
        app.UseHsts();
    }

    // Step 7: Redirect all HTTP requests to HTTPS.
    app.UseHttpsRedirection();

    // Step 8: Enable serving static files (e.g., CSS, JavaScript, images).
    app.UseStaticFiles();

    // Step 9: Enable routing to map requests to controllers and actions.
    app.UseRouting();

    // Step 10: Enable authorization to secure resources.
    app.UseAuthorization();

    // Step 11: Configure the default route for MVC controllers.
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");

    // Step 12: Start the application and listen for incoming requests.
    app.Run();
}

Key Points


  1. Dependency Injection: Services like MVC controllers and views are added to the DI container.

  2. Middleware Pipeline: The pipeline is configured to handle exceptions, enforce HTTPS, serve static files, and enable routing and authorization.

  3. Environment-Specific Configuration: Different configurations are applied based on the environment (e.g., Development, Production).

  4. Routing: The default route maps requests to the HomeController and Index action.

This code sets up a fully functional ASP.NET Core MVC application with secure and efficient request handling.

Comments

Popular posts from this blog

FREE Webinar: Run Your Own Independent DeepSeek LLM

Interview Tips: Dot NET Framework vs Net CORE

Delegates and Events