WASEEM HAIDER’s Post

View profile for WASEEM HAIDER, graphic

.Net Developer/Angular

ASP .Net Core Basic Program Execution Program.Cs file basic code And Detail // Create a builder instance of WebApplicationBuilder class var builder = WebApplication.CreateBuilder(args); // Build the app (Return an instance of WebApplication) var app = builder.Build(); // Create a route - HTTP GET method + URL, returns "Hello World" app.MapGet("/", () => "Hello World!"); // Start the server app.Run(); When we start the execution of our ASP.NET console application, it begins from the `Program.cs` file. This file typically contains the `Program` class and the `Main` method. However, starting with C# 9, Microsoft removed the need for the `Program` class and the `Main` method. Instead, they introduced a simplified approach, and the application can now run with just four lines of code. Details about each line of code are provided in the comments above the code. Detail of each line: code Line 1 // Create a builder instance of WebApplicationBuilder class var builder = WebApplication.CreateBuilder(args); This line creates an instance of WebApplicationBuilder, which sets up configuration, logging, and services for the application. Code Line 2 // Build the app (Return an instance of WebApplication) var app = builder.Build(); After configuring services and middleware, you build the app using builder.Build(). This creates a WebApplication instance that is ready to run. Code Line 3 // Create a route - HTTP GET method + URL, returns "Hello World" app.MapGet("/", () => "Hello World!"); Here, you define a simple route using the MapGet() method. This maps the root URL ("/") to an HTTP GET request, which returns the string "Hello World!". Code Line 4 // Start the server app.Run(); Finally, the app.Run() method starts the web server, allowing it to listen for incoming requests.

To view or add a comment, sign in

Explore topics