Free Web Development Online Tutorials, Learn to Code
Configure a web hosting environment in Program.cs, use of program cs file in asp.net core

Program.cs in ASP.NET Core

What would be the use of Program.cs file in ASP.NET Core application! ASP.NET Core web application requires a host to be executed. We can configure a web hosting environment in Program.cs.

What is Program.cs file?

Program.cs file is the entry point of the application. This will be executed first when the application runs, there is a public static void Main method, Whatever code you write inside that method will be executed in that same order, In asp.net core application we normally call all "hosting related setting and startup.cs file methods" from that Main() method.

Can we run application without Program.cs file ?

No, it will give an build error, will not allow to run the application.

In ASP.NET Core there is a new way to build web host configuration. Now we can set defaults of the web host configuration using WebHost.CreateDefaultBuilder()

This is how the Program.cs file look like
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
public class Program
{
 public static void Main(string[] args)
{
 BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseIISIntegration()
 .UseStartup<Startup>()  
    .Build();
}

In public static void Main() of Program class where we can create a host for the web application, earlier tutorial we have explained about asp.net core Startup.cs class

var host = new WebHostBuilder();
// Now 
IWebHostBuilder host = WebHost.CreateDefaultBuilder(args)
//CreateDefaultBuilder is a static method now create the new instance
// finally call 
host.Run();
            
Comment
Name
Email
Website
Subscribe
 
Program.cs in ASP.NET Core
ASP.NET MVC Interview Questions Answers


Asp.Net Core C# Examples | Join Asp.Net MVC Course