Angular Frontend is not reaching the backend

Discussão em 'Angular' iniciado por Frank Mehlhop, Abril 24, 2025.

  1. In Visual Studio I create a project Angular and ASP.NET core. I start the project the first time and it works correct. Then I start a second time (and more) and the frontend is not showing the WeatherForecast data anymore. I did not made any changes on the project.

    What could cause this problem and how could I solve it?

    I set a breakpoint into the controller public IEnumerable Get(){...} and it is not reached.

    On second and further try I get the error in the output window:


    Angular is running in development mode.

    HttpErrorResponse {headers: _HttpHeaders, status: 500, statusText: 'Internal Server Error', url: 'https://localhost:61284/weatherforecast', ok: false, …}

    [vite] connected.
    ​

    I'm not sure it is about CORS. But I'll share some configurations too.

    Program.cs:

    var builder = WebApplication.CreateBuilder(args);

    // Add services to the container.

    builder.Services.AddControllers();
    // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
    builder.Services.AddOpenApi();

    var app = builder.Build();

    app.UseDefaultFiles();
    app.MapStaticAssets();

    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
    app.MapOpenApi();
    }

    app.UseHttpsRedirection();

    app.UseAuthorization();

    app.MapControllers();

    app.MapFallbackToFile("/index.html");

    app.Run();


    launchSettings.json:

    {
    "$schema": "https://json.schemastore.org/launchsettings.json",
    "profiles": {
    "http": {
    "commandName": "Project",
    "dotnetRunMessages": true,
    "launchBrowser": false,
    "applicationUrl": "http://localhost:5062",
    "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
    }
    },
    "https": {
    "commandName": "Project",
    "dotnetRunMessages": true,
    "launchBrowser": false,
    "applicationUrl": "https://localhost:7052;http://localhost:5062",
    "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
    }
    }
    }
    }


    package.json

    {
    "name": "angularapp1.client",
    "version": "0.0.0",
    "scripts": {
    "ng": "ng",
    "start": "run-script-os",
    "build": "ng build",
    "watch": "ng build --watch --configuration development",
    "test": "ng test",
    "prestart": "node aspnetcore-https",
    "start:windows": "ng serve --ssl --ssl-cert \"%APPDATA%\\ASP.NET\\https\\%npm_package_name%.pem\" --ssl-key \"%APPDATA%\\ASP.NET\\https\\%npm_package_name%.key\" --host=127.0.0.1",
    "start:default": "ng serve --ssl --ssl-cert \"$HOME/.aspnet/https/${npm_package_name}.pem\" --ssl-key \"$HOME/.aspnet/https/${npm_package_name}.key\" --host=127.0.0.1"
    },
    "private": true,
    "dependencies": {
    "@angular/common": "^19.2.0",
    "@angular/compiler": "^19.2.0",
    "@angular/core": "^19.2.0",
    "@angular/forms": "^19.2.0",
    "@angular/platform-browser": "^19.2.0",
    "@angular/platform-browser-dynamic": "^19.2.0",
    "@angular/router": "^19.2.0",
    "rxjs": "~7.8.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.15.0",
    "jest-editor-support": "*",
    "run-script-os": "*"
    },
    "devDependencies": {
    "@angular-devkit/build-angular": "^19.2.9",
    "@angular/cli": "^19.2.9",
    "@angular/compiler-cli": "^19.2.0",
    "@types/jasmine": "~5.1.0",
    "jasmine-core": "~5.6.0",
    "karma": "~6.4.0",
    "karma-chrome-launcher": "~3.2.0",
    "karma-coverage": "~2.2.0",
    "karma-jasmine": "~5.1.0",
    "karma-jasmine-html-reporter": "~2.1.0",
    "typescript": "~5.7.2"
    }
    }


    proxy.conf.js:

    const { env } = require('process');

    const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` :
    env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'https://localhost:7052';

    const PROXY_CONFIG = [
    {
    context: [
    "/weatherforecast",
    ],
    target,
    secure: false
    }
    ]

    module.exports = PROXY_CONFIG;

    Continue reading...

Compartilhe esta Página