In previous versions of .NET, a console application has a Main() method and it is the entry point for that console application.
Why we need Main() method in ASP.NET core application?
Asp.net core application initially starts as a console application and the Main() method in Program.cs file is the entry point. When the runtime executes application it looks for this Main() method and this where the execution starts.
This Main() method configures asp.net core and starts it and at that point, it becomes an asp.net core web application.
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
Startup class in ASP.NET Core
Startup class is configured using the UseStartup() extension method of IWebHostBuilder class. The startup class in ASP.NET Core is named Startup. This class has 2 methods.
- ConfigureServices() method configures services required by the application
- Configure() method sets up the application's request processing pipeline
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{ }
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
Comments
Post a Comment