A full-stack app setup using:
-
๐ Angular 17+ frontend
-
๐ฅ๏ธ ASP.NET Core 9 Web API backend
-
๐งฐ Visual Studio Code as your IDE
๐ฆ Prerequisites
Make sure you have the following tools installed:
Tool | Description | Link |
---|---|---|
.NET 9 SDK | For backend | |
Node.js | Needed for Angular and npm | |
Angular CLI | Run: npm install -g @angular/cli |
|
VS Code | Your development editor | |
C# Extension for VS Code | C# support | |
REST Client or Thunder Client | Optional for testing APIs |
๐ง Step 1: Create the Backend (ASP.NET Core 9 API)
Open terminal in VS Code and run:
dotnet new webapi -n BackendApp
cd BackendApp
Optional: Enable CORS
Edit Program.cs
:
builder.Services.AddCors(options => { options.AddPolicy("AllowAngular", builder => { builder.WithOrigins("http://localhost:4200") .AllowAnyHeader() .AllowAnyMethod(); }); });
Then use it in the pipeline:
app.UseCors("AllowAngular");
โ Save the file.
๐ Step 2: Run the API in VS Code
In terminal:
dotnet run
-
Test: open
https://localhost:5001/api/weatherforecast
in your browser -
Or use Thunder Client (extension) to test it in VS Code
๐งฑ Step 3: Create the Angular Frontend
In a new terminal window (not inside BackendApp
), run:
ng new frontend-app --routing --style=scss
cd frontend-app
code .
โ Choose "Yes" to Angular routing
โ Choose "SCSS" or "CSS" for styling
๐ Step 4: Connect Angular to .NET API
First, install HttpClientModule
if not already present:
In app.module.ts
:
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
HttpClientModule,
// ...
]
})
Generate a service:
ng generate service services/weather
Inside weather.service.ts
:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class WeatherService {
private apiUrl = 'https://localhost:5001/api/weatherforecast';
constructor(private http: HttpClient) {} getWeather(): Observable<any> { return this.http.get(this.apiUrl); }
}
๐บ Step 5: Use the Service in Angular
In app.component.ts
:
import { Component, OnInit } from '@angular/core';
import { WeatherService } from './services/weather.service';
@Component({
selector: 'app-root',
template: ` <h2>Weather Forecast</h2> <ul> <li *ngFor="let item of weather">{{ item.date }} - {{ item.temperatureC }}°C</li> </ul> `
})
export class AppComponent implements OnInit {
weather: any[] = [];
constructor(private weatherService: WeatherService) {} ngOnInit() {
this.weatherService.getWeather().subscribe(data => { this.weather = data; }); }
}
๐งช Step 6: Run the Angular App
ng serve
Open: http://localhost:4200
Make sure your backend is also running at https://localhost:5001
.
๐ฆ Optional: Serve Angular with .NET Core in Production
-
Build Angular:
ng build --configuration production
-
Copy contents of
dist/frontend-app
intoBackendApp/wwwroot
-
Update
Program.cs
:app.UseDefaultFiles(); app.UseStaticFiles(); app.MapFallbackToFile("index.html");
-
Run the backend. It now serves Angular too!
โ Final Folder Structure
/MyFullStackApp
โโโ /BackendApp (ASP.NET Core 9)
โ โโโ wwwroot (Angular dist here)
โโโ /frontend-app (Angular project)