ASP.NET CORE MVC - Program.cs
Program.cs
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 isHomeController
.action=Index
: The default action isIndex
.id?
: Theid
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.
Key Points
Dependency Injection: Services like MVC controllers and views are added to the DI container.
Middleware Pipeline: The pipeline is configured to handle exceptions, enforce HTTPS, serve static files, and enable routing and authorization.
Environment-Specific Configuration: Different configurations are applied based on the environment (e.g., Development, Production).
Routing: The default route maps requests to the
HomeController
andIndex
action.
This code sets up a fully functional ASP.NET Core MVC application with secure and efficient request handling.
Comments
Post a Comment