Skip to content

Commit 8d84931

Browse files
committed
feat: add comprehensive guidelines for building REST APIs with ASP.NET and Blazor components
1 parent 70a9641 commit 8d84931

9 files changed

Lines changed: 1434 additions & 0 deletions
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
description: 'Guidelines for building REST APIs with ASP.NET'
3+
applyTo: '**/*.cs, **/*.json'
4+
---
5+
6+
# ASP.NET REST API Development
7+
8+
## Instruction
9+
- Guide users through building their first REST API using ASP.NET Core 9.
10+
- Explain both traditional Web API controllers and the newer Minimal API approach.
11+
- Provide educational context for each implementation decision to help users understand the underlying concepts.
12+
- Emphasize best practices for API design, testing, documentation, and deployment.
13+
- Focus on providing explanations alongside code examples rather than just implementing features.
14+
15+
## API Design Fundamentals
16+
17+
- Explain REST architectural principles and how they apply to ASP.NET Core APIs.
18+
- Guide users in designing meaningful resource-oriented URLs and appropriate HTTP verb usage.
19+
- Demonstrate the difference between traditional controller-based APIs and Minimal APIs.
20+
- Explain status codes, content negotiation, and response formatting in the context of REST.
21+
- Help users understand when to choose Controllers vs. Minimal APIs based on project requirements.
22+
23+
## Project Setup and Structure
24+
25+
- Guide users through creating a new ASP.NET Core 9 Web API project with the appropriate templates.
26+
- Explain the purpose of each generated file and folder to build understanding of the project structure.
27+
- Demonstrate how to organize code using feature folders or domain-driven design principles.
28+
- Show proper separation of concerns with models, services, and data access layers.
29+
- Explain the Program.cs and configuration system in ASP.NET Core 9 including environment-specific settings.
30+
31+
## Building Controller-Based APIs
32+
33+
- Guide the creation of RESTful controllers with proper resource naming and HTTP verb implementation.
34+
- Explain attribute routing and its advantages over conventional routing.
35+
- Demonstrate model binding, validation, and the role of [ApiController] attribute.
36+
- Show how dependency injection works within controllers.
37+
- Explain action return types (IActionResult, ActionResult<T>, specific return types) and when to use each.
38+
39+
## Implementing Minimal APIs
40+
41+
- Guide users through implementing the same endpoints using the Minimal API syntax.
42+
- Explain the endpoint routing system and how to organize route groups.
43+
- Demonstrate parameter binding, validation, and dependency injection in Minimal APIs.
44+
- Show how to structure larger Minimal API applications to maintain readability.
45+
- Compare and contrast with controller-based approach to help users understand the differences.
46+
47+
## Data Access Patterns
48+
49+
- Guide the implementation of a data access layer using Entity Framework Core.
50+
- Explain different options (SQL Server, SQLite, In-Memory) for development and production.
51+
- Demonstrate repository pattern implementation and when it's beneficial.
52+
- Show how to implement database migrations and data seeding.
53+
- Explain efficient query patterns to avoid common performance issues.
54+
55+
## Authentication and Authorization
56+
57+
- Guide users through implementing authentication using JWT Bearer tokens.
58+
- Explain OAuth 2.0 and OpenID Connect concepts as they relate to ASP.NET Core.
59+
- Show how to implement role-based and policy-based authorization.
60+
- Demonstrate integration with Microsoft Entra ID (formerly Azure AD).
61+
- Explain how to secure both controller-based and Minimal APIs consistently.
62+
63+
## Validation and Error Handling
64+
65+
- Guide the implementation of model validation using data annotations and FluentValidation.
66+
- Explain the validation pipeline and how to customize validation responses.
67+
- Demonstrate a global exception handling strategy using middleware.
68+
- Show how to create consistent error responses across the API.
69+
- Explain problem details (RFC 7807) implementation for standardized error responses.
70+
71+
## API Versioning and Documentation
72+
73+
- Guide users through implementing and explaining API versioning strategies.
74+
- Demonstrate Swagger/OpenAPI implementation with proper documentation.
75+
- Show how to document endpoints, parameters, responses, and authentication.
76+
- Explain versioning in both controller-based and Minimal APIs.
77+
- Guide users on creating meaningful API documentation that helps consumers.
78+
79+
## Logging and Monitoring
80+
81+
- Guide the implementation of structured logging using Serilog or other providers.
82+
- Explain the logging levels and when to use each.
83+
- Demonstrate integration with Application Insights for telemetry collection.
84+
- Show how to implement custom telemetry and correlation IDs for request tracking.
85+
- Explain how to monitor API performance, errors, and usage patterns.
86+
87+
## Testing REST APIs
88+
89+
- Guide users through creating unit tests for controllers, Minimal API endpoints, and services.
90+
- Explain integration testing approaches for API endpoints.
91+
- Demonstrate how to mock dependencies for effective testing.
92+
- Show how to test authentication and authorization logic.
93+
- Explain test-driven development principles as applied to API development.
94+
95+
## Performance Optimization
96+
97+
- Guide users on implementing caching strategies (in-memory, distributed, response caching).
98+
- Explain asynchronous programming patterns and why they matter for API performance.
99+
- Demonstrate pagination, filtering, and sorting for large data sets.
100+
- Show how to implement compression and other performance optimizations.
101+
- Explain how to measure and benchmark API performance.
102+
103+
## Deployment and DevOps
104+
105+
- Guide users through containerizing their API using .NET's built-in container support (`dotnet publish --os linux --arch x64 -p:PublishProfile=DefaultContainer`).
106+
- Explain the differences between manual Dockerfile creation and .NET's container publishing features.
107+
- Explain CI/CD pipelines for ASP.NET Core applications.
108+
- Demonstrate deployment to Azure App Service, Azure Container Apps, or other hosting options.
109+
- Show how to implement health checks and readiness probes.
110+
- Explain environment-specific configurations for different deployment stages.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
description: 'Blazor component and application patterns'
3+
applyTo: '**/*.razor, **/*.razor.cs, **/*.razor.css'
4+
---
5+
6+
## Blazor Code Style and Structure
7+
8+
- Write idiomatic and efficient Blazor and C# code.
9+
- Follow .NET and Blazor conventions.
10+
- Use Razor Components appropriately for component-based UI development.
11+
- Prefer inline functions for smaller components but separate complex logic into code-behind or service classes.
12+
- Async/await should be used where applicable to ensure non-blocking UI operations.
13+
14+
## Naming Conventions
15+
16+
- Follow PascalCase for component names, method names, and public members.
17+
- Use camelCase for private fields and local variables.
18+
- Prefix interface names with "I" (e.g., IUserService).
19+
20+
## Blazor and .NET Specific Guidelines
21+
22+
- Utilize Blazor's built-in features for component lifecycle (e.g., OnInitializedAsync, OnParametersSetAsync).
23+
- Use data binding effectively with @bind.
24+
- Leverage Dependency Injection for services in Blazor.
25+
- Structure Blazor components and services following Separation of Concerns.
26+
- Always use the latest version C#, currently C# 13 features like record types, pattern matching, and global usings.
27+
28+
## Error Handling and Validation
29+
30+
- Implement proper error handling for Blazor pages and API calls.
31+
- Use logging for error tracking in the backend and consider capturing UI-level errors in Blazor with tools like ErrorBoundary.
32+
- Implement validation using FluentValidation or DataAnnotations in forms.
33+
34+
## Blazor API and Performance Optimization
35+
36+
- Utilize Blazor server-side or WebAssembly optimally based on the project requirements.
37+
- Use asynchronous methods (async/await) for API calls or UI actions that could block the main thread.
38+
- Optimize Razor components by reducing unnecessary renders and using StateHasChanged() efficiently.
39+
- Minimize the component render tree by avoiding re-renders unless necessary, using ShouldRender() where appropriate.
40+
- Use EventCallbacks for handling user interactions efficiently, passing only minimal data when triggering events.
41+
42+
## Caching Strategies
43+
44+
- Implement in-memory caching for frequently used data, especially for Blazor Server apps. Use IMemoryCache for lightweight caching solutions.
45+
- For Blazor WebAssembly, utilize localStorage or sessionStorage to cache application state between user sessions.
46+
- Consider Distributed Cache strategies (like Redis or SQL Server Cache) for larger applications that need shared state across multiple users or clients.
47+
- Cache API calls by storing responses to avoid redundant calls when data is unlikely to change, thus improving the user experience.
48+
49+
## State Management Libraries
50+
51+
- Use Blazor's built-in Cascading Parameters and EventCallbacks for basic state sharing across components.
52+
- Implement advanced state management solutions using libraries like Fluxor or BlazorState when the application grows in complexity.
53+
- For client-side state persistence in Blazor WebAssembly, consider using Blazored.LocalStorage or Blazored.SessionStorage to maintain state between page reloads.
54+
- For server-side Blazor, use Scoped Services and the StateContainer pattern to manage state within user sessions while minimizing re-renders.
55+
56+
## API Design and Integration
57+
58+
- Use HttpClient or other appropriate services to communicate with external APIs or your own backend.
59+
- Implement error handling for API calls using try-catch and provide proper user feedback in the UI.
60+
61+
## Testing and Debugging in Visual Studio
62+
63+
- All unit testing and integration testing should be done in Visual Studio Enterprise.
64+
- Test Blazor components and services using xUnit, NUnit, or MSTest.
65+
- Use Moq or NSubstitute for mocking dependencies during tests.
66+
- Debug Blazor UI issues using browser developer tools and Visual Studio's debugging tools for backend and server-side issues.
67+
- For performance profiling and optimization, rely on Visual Studio's diagnostics tools.
68+
69+
## Security and Authentication
70+
71+
- Implement Authentication and Authorization in the Blazor app where necessary using ASP.NET Identity or JWT tokens for API authentication.
72+
- Use HTTPS for all web communication and ensure proper CORS policies are implemented.
73+
74+
## API Documentation and Swagger
75+
76+
- Use Swagger/OpenAPI for API documentation for your backend API services.
77+
- Ensure XML documentation for models and API methods for enhancing Swagger documentation.

0 commit comments

Comments
 (0)