Introduction
Connecting an Angular frontend with an ASP.NET Core Web API is a common architecture pattern for building modern, scalable, and high-performance web applications. Angular handles the client-side user interface, while ASP.NET Core Web API manages backend logic, business rules, authentication, and database operations. This separation enables clean architecture, independent deployment, and better maintainability in enterprise-grade applications.
Understanding the Architecture
In a typical Angular and ASP.NET Core integration:
Angular acts as the frontend SPA (Single Page Application).
ASP.NET Core Web API exposes RESTful endpoints.
Communication happens over HTTP using JSON.
The backend usually connects to a database using Entity Framework Core.
The frontend consumes API endpoints using Angular’s HttpClient module.
Step 1: Create ASP.NET Core Web API
Create a new Web API project:
dotnet new webapi -n MyApp.Api
Ensure CORS (Cross-Origin Resource Sharing) is enabled because Angular and ASP.NET Core typically run on different ports during development.
Inside Program.cs:
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAngularApp",
policy => policy
.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod());
});
app.UseCors("AllowAngularApp");
Create a sample controller:
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return Ok(new[] { "Laptop", "Mobile", "Tablet" });
}
}
Run the API and verify the endpoint works at:
https://localhost:5001/api/products
Step 2: Create Angular Application
Create a new Angular project:
ng new myapp-client
cd myapp-client
ng serve
Angular runs by default at:
http://localhost:4200
Step 3: Import HttpClientModule
Open app.module.ts and import HttpClientModule:
import { HttpClientModule } from '@angular/common/http';
@NgModule({
imports: [
BrowserModule,
HttpClientModule
]
})
export class AppModule {}
This module enables Angular to communicate with REST APIs.
Step 4: Create Angular Service for API Calls
Generate a service:
ng generate service services/product
Inside product.service.ts:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class ProductService {
private apiUrl = 'https://localhost:5001/api/products';
constructor(private http: HttpClient) {}
getProducts(): Observable<string[]> {
return this.http.get<string[]>(this.apiUrl);
}
}

Join the conversation! Your thoughts help the community grow.