Designs ASP.NET Core application composition, middleware, dependency injection, controllers, hosting, options, authentication, and authorization. Use for application-wide web-host concerns; do not use for Minimal API endpoint design (use minimal-api-patterns) or standalone contract review (use api-contract-review).
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Aspnetcore Patterns?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/k4ndev-aspnetcore-patterns)
---
name: aspnetcore-patterns
description: Designs ASP.NET Core application composition, middleware, dependency injection, controllers, hosting, options, authentication, and authorization. Use for application-wide web-host concerns; do not use for Minimal API endpoint design (use minimal-api-patterns) or standalone contract review (use api-contract-review).
---
# ASP.NET Core Patterns
## Purpose
Build web-host behavior with correct middleware order, DI lifetimes, configuration validation, security boundaries, and operations readiness.
## Scope
Application-level ASP.NET Core composition, MVC/controllers, middleware, options, outbound HTTP clients, health checks, and hosting. Minimal API endpoint shape belongs to `minimal-api-patterns`.
## When to use
- A task concerns middleware ordering, DI, configuration, controller organization, authentication setup, or web-host readiness.
- A web app needs an integration-testing strategy for host-level behavior.
## Do not use for
- A Minimal API route handler/group as the primary subject: use `minimal-api-patterns`.
- API compatibility/status semantics only: use `api-contract-review`.
- Threat modeling across the whole application: coordinate with `security-audit`.
## Required context
Inspect target framework/package versions, `Program.cs`, middleware order, registrations and service lifetimes, options/configuration sources, auth policies, controllers, error contract, deployment model, and existing integration tests.
## Workflow
1. Map request flow from server to exception handling, routing, authentication, authorization, endpoint, and response; compare actual middleware order with the app's requirements.
2. Trace service lifetimes. A singleton must not capture a scoped service; do not solve it by turning request state into singleton state.
3. Bind critical options and validate them at startup (for supported target versions, `ValidateOnStart`); avoid logging secret values.
4. Keep controllers focused on HTTP concerns and use explicit request/response DTOs at external boundaries.
5. Enforce authorization server-side, including resource ownership; CORS is not authorization.
6. Use `IHttpClientFactory` or typed clients for outbound HTTP; configure timeouts/resilience intentionally and avoid unbounded retries.
7. Return the established error contract/ProblemDetails without stack traces or infrastructure details; bound request size and pagination.
8. Add host-level integration tests for the changed middleware, auth, serialization, or routing behavior; check health readiness semantics.
## Decision rules
- Preserve existing architecture and error conventions unless they are demonstrably unsafe.
- Use middleware for cross-cutting request-pipeline behavior, filters for narrower MVC behavior, and application services for business logic.
- Verify .NET/package versions before relying on framework APIs; do not infer security from CORS or client-side hiding.
## Expected output format
State request/host flow, lifetime/configuration/security risks, the minimal code or registration change, relevant version constraints, and integration tests. Identify unverified environment assumptions.
## Common mistakes
- Registering a scoped repository in a singleton, placing authorization before authentication, exposing detailed errors, or binding unvalidated secrets/options.
- Creating `new HttpClient()` per operation, using CORS as access control, and testing middleware only with isolated unit tests.
## Edge cases
- Startup validation may delay host startup by design; explain rollout implications.
- `IOptionsSnapshot<T>` is scoped and unsuitable for singleton consumption; `IOptionsMonitor<T>` has different change semantics.
- Blazor Server/Interactive Server combines UI concerns with an HTTP host; classify the actual hosting model.
## Examples
See [ASP.NET Core examples](examples.md) for validated options and safe outbound clients.
## Test scenarios
- Positive: “Our singleton hosted service captures a scoped DbContext; propose a safe design.”
- Negative: “Optimize this EF query's projection” routes to `efcore-patterns` / `performance-review`.
- Boundary: “Review this MVC endpoint's response compatibility” selects `api-contract-review` and `dotnet-code-review` only when review intent is present.
## Related skills
`minimal-api-patterns`, `api-contract-review`, `security-audit`, `logging-observability`, `async-concurrency`, `testing-quality`.