I looked through the documentation on the Microsoft website and there are two places where we can set up the configuration.
We can do it either in Startup.cs or Program.cs. However, Program.cs has the same methods that are available in Startup.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureServices(services =>
{
//same as ConfigureServices method in Startup.cs
services.AddAutofac();
})
.Configure(app =>
{
//same as Configure method in Startup.cs
app.UseMvc();
})
.Build();
}
Is the only purpose for the existence of "Startup.cs" to move some of the configuration out of "Program.cs"? Could we remove this file altogether and keep the entire configuration in "Program.cs"?
ASP.NET Core apps created with the web templates contain the application startup code in the Program. cs file. The following app startup code supports: Razor Pages. MVC controllers with views.
ASP.NET Core web application is actually a console project which starts executing from the entry point public static void Main() in Program class where we can create a host for the web application.
The Startup class in .NET and .NET Core The Startup class contains the ConfigureServices and Configure methods. While the former is used to configure the required services, the latter is used to configure the request processing pipeline. The Configure method is executed immediately after the ConfigureServices method.
Is Startup Class mandatory? Yes, this class is mandatory in ASP.net core application. It can have any access modifier (public, private, internal). This is the entry point of the ASP.net application.
Could we remove this class altogether and keep entire configuration in Program.cs ?
Yes
Documentation explains
Convenience methods
To configure services and the request processing pipeline without using a
Startup
class, callConfigureServices
andConfigure
convenience methods on the host builder. Multiple calls toConfigureServices
append to one another. If multipleConfigure
method calls exist, the lastConfigure
call is used.
It is more about the configuring of the builder than the actual Program.cs. That is just the default template class used to hold main entry to the application.
Reference App startup in ASP.NET Core
Program.cs is where the application starts.
Startup.cs is where lot of the configuration happens.
The idea for this separation is based on SOLID principles' first principle- SRP (Single Responsibility Principle). SOLID principles make software designs more understandable, flexible, and maintainable.
Single Responsibility Principle (SRP) states that a class or a method should only do one thing (or should have only one job). If you look at the Startup.cs, it does exactly this making it easy to read and understand the code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With