skip to Main Content

There is no startup.cs application in my dotnet core 8 project. The guide I followed includes startup.cs because it is an old version. What should I do?

In the guide, there is CORS disablement, but I do not have startup.cs.

2

Answers


  1. In .NET Core 3.x and later, there is no Startup.cs file by default in ASP.NET Core projects. Instead, you typically configure your application using the Program.cs file and the WebHostBuilder. Here’s a basic example of how you can configure a .NET Core 3.x or later application:

    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }
    
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.ConfigureServices((context, services) =>
                    {
                        // Configure services here
                    });
    
                    webBuilder.Configure((app) =>
                    {
                        // Configure app middleware here
                    });
                });
    }
    

    In this setup, you can configure services and middleware directly within the ConfigureServices and Configure methods, respectively. You don’t have a separate Startup.cs file for this configuration.

    Login or Signup to reply.
  2. Since .NET 6 default ASP.NET Core templates use so called minimal hosting model. But you can still use the one with Startup – see the .NET Generic Host in ASP.NET Core doc.

    If you want to follow the new approach the general rule of thumb would be to move everything from Startup.ConfigureServices to before building the app (i.e. var app = builder.Build();) call and use builder.Services and buidler.Configuration where appropriate and everything from Startup.Configure – after building the app and use the app for corresponding calls (check out code generated by the default template).

    See also:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search