How to Create a Modern Web App Using Angular and ASP.NET Core 9 (with VS Code)?

Views: 32
Comments: 0
Like/Unlike: 0
Posted On: 29-Jul-2025 04:42 

Share:   fb twitter linkedin
Priya
1288 Points
38 Posts

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

  1. Build Angular:

     
    ng build --configuration production
  2. Copy contents of dist/frontend-app into BackendApp/wwwroot

  3. Update Program.cs:

     
    app.UseDefaultFiles(); app.UseStaticFiles(); app.MapFallbackToFile("index.html");
  4. 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)

 
 
0 Comments
 Log In to Chat