Skip to content
Back to skills

Aspnet Core

ASecurity

Use when creating or modifying C# files under src/backend/ in an ASP.NET Core service — Program bootstrap, minimal-API endpoints or controllers, services, middleware, DI registrations, DTOs, and their tests. Triggers on any .cs change under src/backend/. Covers DI/container wiring, thin endpoints, the global exception-handling middleware, fail-closed DTO validation, the options pattern for config, and service testing with WebApplicationFactory. Also known as dotnet.

  • 6 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 4, 2026
developmentc#testingapibackend

Works with

  • cli
  • api

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned September 4, 2026

npx -y skills add kouroshez/coding-os --skill aspnet-core --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Aspnet Core?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Aspnet Core
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/kouroshez-aspnet-core/badge)](https://www.skillsdirectory.com/skills/kouroshez-aspnet-core)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: aspnet-core
tier: stack
domain: [backend]
description: Use when creating or modifying C# files under src/backend/ in an ASP.NET Core service — Program bootstrap, minimal-API endpoints or controllers, services, middleware, DI registrations, DTOs, and their tests. Triggers on any .cs change under src/backend/. Covers DI/container wiring, thin endpoints, the global exception-handling middleware, fail-closed DTO validation, the options pattern for config, and service testing with WebApplicationFactory. Also known as dotnet.
globs: "src/backend/**/*.cs"
depends_on:
  - clean-code
  - backend-fundamentals
  - api-design
last_reviewed: "2026-06-14"
---

REQUIRED BACKGROUND: You MUST also follow the core `clean-code` and
`backend-fundamentals` skills. This skill adds ASP.NET Core-specific patterns
on top.

# aspnet-core

## Layer contract (matches `structure.tree`)

| Layer | May import | Never |
|---|---|---|
| `*Endpoints.cs` / controllers | the feature service, DTOs | repositories, other endpoints |
| `*Service.cs` | repositories, other services | `HttpContext`, `IHttpContextAccessor`, transport types |
| `Repositories/` | the `DbContext` / DB client | services, endpoints |
| `Common/` (middleware/filters) | services (for auth lookups) | repositories |

Services stay transport-free — no `HttpContext` — so they are unit-testable and
a transport swap (minimal API → controllers → gRPC) is an endpoint-layer-only
change.

## DI & wiring

- Register every service in `Program.cs` (`AddScoped`/`AddSingleton`/`AddTransient`);
  resolve by constructor or endpoint-parameter injection — never `new` a service.
- `Program.cs` owns wiring only, not business logic. One `WebApplication`,
  middleware pipeline registered once, feature endpoints mapped via
  `<Feature>Endpoints.Map(app)`.
- Lifetimes: `Scoped` for per-request (the default for services touching the DB),
  `Singleton` only for stateless/thread-safe helpers, `Transient` for cheap
  stateless objects. A captive dependency (Singleton holding a Scoped) is a bug.
- Keep `Program` reachable to tests (`public partial class Program;`) so
  `WebApplicationFactory<Program>` can build the app without binding a port.

## Endpoints (thin)

- A handler binds/validates the DTO → calls ONE service method → returns the
  value (the host serializes). Do not build the response envelope by hand.
- No try/catch for error shaping in an endpoint — throw a typed exception and let
  the global middleware shape it.
- Flow `CancellationToken` from the handler into every downstream call.

## Validation

- Every body/route/query input is a validated DTO (DataAnnotations or
  FluentValidation); reject fail-closed before the service runs. The service
  receives a typed, validated value and never reads the raw request.

## Error handling

- ONE global `ExceptionHandlingMiddleware` (`Common/ExceptionHandlingMiddleware.cs`)
  shapes every error response (RFC 9457 problem shape per
  `docs/api-contracts/error-format.md`). Unknown exceptions → 500 generic
  message, full detail to the logger only — never a stack trace to the client.
- Register it first in the pipeline (`app.UseMiddleware<ExceptionHandlingMiddleware>()`)
  so it wraps all downstream handlers.

## Configuration

- Bind configuration once at bootstrap via the options pattern (`IOptions<T>`);
  services receive typed options and never read raw configuration keys deep in a
  method.

## Testing

- Services: pure unit tests, no host — construct with fakes/mocks and assert.
- Endpoints/integration: `WebApplicationFactory<Program>` builds the app
  in-memory; one happy + one error-path per endpoint minimum.
- Never bind a real port in tests; always build through the factory.

Files in this skill

  • SKILL.md3.7 KB
  • references/anatomy.md2.7 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.