Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Startup.cs vs Program.cs in ASP.NET Core 2

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"?

like image 347
cah1r Avatar asked Feb 21 '19 09:02

cah1r


People also ask

What is startup cs in ASP.NET Core?

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.

What is the use of Program cs in .NET Core?

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.

What is the role of startup class in .NET Core?

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 in ASP.NET Core?

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.


2 Answers

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, call ConfigureServices and Configure convenience methods on the host builder. Multiple calls to ConfigureServices append to one another. If multiple Configure method calls exist, the last Configure 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

like image 167
Nkosi Avatar answered Oct 15 '22 17:10

Nkosi


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.

like image 29
j4jada Avatar answered Oct 15 '22 15:10

j4jada