Merge Startup.cs and Program.cs on updating Angular app from .net 5 to .net 8 cause 404 errors

Discussão em 'Angular' iniciado por Electrionics, Janeiro 16, 2025.

  1. Electrionics

    Electrionics Guest

    I have several projects and decided to keep same .NET version to avoid infrastructure hell and be able to use common code (logging setup etc.) in similar way.

    Despite 2 of them created from Angular template and 2 other created from Blazor template - idea looks good.

    As a start I updated version of ASP.NET Core Angular app .NET version from 5 to 8 and merged Startup.cs and Program.cs.

    For testing I added Serilog to my ASP.NET Core app, like it already done in another Blazor web app (.NET 8).

    Right now app runs but after merging, like described here, client (Angular) app requests didn't found my controller API method and returns 404 http error.

    Below is code of updated Program.cs:

    var builder = WebApplication.CreateBuilder(args);

    var databaseConfig = builder.Configuration.GetSection("Database").Get<DatabaseConfig>();

    builder.Services.AddControllers(options =>
    {
    options.Filters.Add(new SessionIdFilter());
    }).AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = null);

    // In production, the Angular files will be served from this directory
    builder.Services.AddSpaStaticFiles(configuration => { configuration.RootPath = "ClientApp/dist"; });

    builder.Services.AddSingleton(databaseConfig);

    builder.Services.AddDbContext<NumberDataContext>(options =>
    {
    options.UseSqlServer(databaseConfig.ConnectionString);
    });

    builder.Services.AddSession(options => {
    options.Cookie.Name = "SID";
    options.IdleTimeout = TimeSpan.FromDays(30);
    });

    builder.Services.Configure<CookiePolicyOptions>(options =>
    {
    options.CheckConsentNeeded = _ => false;
    });

    builder.Services.AddMvc(setup => {
    }).AddFluentValidation();

    builder.Services.AddTransient<IValidator<UpdateEndedModel>, UpdateEndedModelValidator>();

    builder.Services.AddScoped<ICheckService, CheckService>();
    builder.Services.AddScoped<IGameService, GameService>();
    builder.Services.AddScoped<ITutorialService, TutorialService>();

    var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration)
    .Enrich.FromLogContext()
    .CreateLogger();
    builder.Logging.ClearProviders();
    builder.Logging.AddSerilog(logger);
    builder.Host.UseSerilog(logger);

    var app = builder.Build();

    if (app.Environment.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }
    else
    {
    app.UseExceptionHandler("/Error");
    app.UseHsts();
    }

    app.UseSession();

    app.UseHttpsRedirection();
    app.UseStaticFiles();

    if (!app.Environment.IsDevelopment())
    {
    app.UseSpaStaticFiles();
    }

    app.UseRouting();

    app.UseSpa(spa =>
    {
    spa.Options.SourcePath = "ClientApp";

    if (app.Environment.IsDevelopment())
    {
    spa.UseAngularCliServer(npmScript: "start");
    }
    });

    app.MapControllers();

    app.Run();


    No code was removed due to merging, except this:

    public static void Main(string[] args)
    {
    CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });

    Continue reading...

Compartilhe esta Página