ASP.NET Core Web API Backend
The backend is built using ASP.NET Core Web API, providing a RESTful API for the Blazor frontend to consume.Project Structure
Technology Stack
Core Framework
- .NET 9.0 with ASP.NET Core Web API
- C# 11 with nullable reference types
- Entity Framework Core 9.0 - ORM for data access
- SQL Server - Database provider
Key NuGet Packages
Authentication & Security:Microsoft.AspNetCore.Authentication.JwtBearer(v9.0.4) - JWT authenticationMicrosoft.IdentityModel.Tokens- Token validation
Microsoft.EntityFrameworkCore.SqlServer(v9.0.4) - SQL Server providerMicrosoft.EntityFrameworkCore.Tools(v9.0.4) - Migration toolsMicrosoft.EntityFrameworkCore.Design(v9.0.4) - Design-time support
Microsoft.AspNetCore.OpenApi(v9.0.4) - OpenAPI specificationScalar.AspNetCore(v2.1.13) - Modern API documentation UI
QuestPDF(v2023.12.3) - PDF document creation
Microsoft.VisualStudio.Web.CodeGeneration.Design(v9.0.0) - Scaffolding
Application Entry Point
TheProgram.cs file configures the API services and middleware:
Service Configuration
CORS Configuration
JWT Authentication
Middleware Pipeline
- CORS policy
- HTTPS redirection
- Authentication
- Authorization
- Controller routing
Controllers
AuthController
Location:Controllers/AuthController.cs
Responsibilities:
- User login and authentication
- JWT token generation
- User validation
POST /api/auth/login- User login- Returns JWT token on successful authentication
- Validate user credentials against database
- Create security claims (user ID, username, role)
- Generate JWT token with configured secret key
- Return token in
LoginResponseDTO
ExpedienteDTOesController
Location:Controllers/ExpedienteDTOesController.cs
Responsibilities:
- CRUD operations for expedientes (requests/cases)
- Search and filtering
- Status management
- Business logic for request processing
GET /api/expedientes- List all expedientesGET /api/expedientes/{id}- Get single expedientePOST /api/expedientes- Create new expedientePUT /api/expedientes/{id}- Update expedienteDELETE /api/expedientes/{id}- Delete expediente
CalendarioController
Location:Controllers/CalendarioController.cs
Responsibilities:
- Calendar management
- Non-working days (holidays)
- Deadline calculations
- Manual date adjustments
GET /api/calendario- Get calendar entriesPOST /api/calendario- Add calendar entryPUT /api/calendario/{id}- Update calendar entryDELETE /api/calendario/{id}- Delete calendar entry
RecursoRevisionController
Location:Controllers/RecursoRevisionController.cs
Responsibilities:
- Review resource management
- Document tracking
- Statistical reports
- Export functionality
- Resource CRUD operations
- Search and filtering
- Statistics generation
- Report exports
Data Access Layer
SistemaSolicitudesContext
Location:Models/SistemaSolicitudesContext.cs
The DbContext serves as the data access layer:
- Fluent API in
OnModelCreating - Custom table mappings
- Property configurations
- Relationships and constraints
Entity Models
Expediente Entity
Purpose: Represents a request or case in the system Key Properties:Id- Primary keyFolio- Unique case number (max 50 chars)NombreSolicitante- Applicant name (max 50 chars)ContenidoSolicitud- Request content (nvarchar(max))Estado- Status (max 50 chars)FechaInicio- Start date (datetime)SubsanaPrevencionReinicoTramite- Process restart flag
Usuario Entity
Purpose: System users and authentication Key Properties:Id- Primary keyNombreUsuario- Username (max 50 chars)password- Hashed password (max 4000 chars)Rol- User role (max 50 chars)
Calendario Entity
Purpose: Calendar entries for deadline tracking Key Properties:Id- Primary key- Calendar-specific fields (dates, events)
DiaInhabilManual Entity
Purpose: Manually defined non-working days Key Properties:- Date information
- Description or reason
PDF Generation
QuestPDF Integration:- Community license configured in
Program.cs - PDF generation services in
PDF/directory - Document templates for reports
- Export functionality for expedientes and statistics
Configuration Management
appsettings.json
ConnectionStrings- Database connectionsJwt- JWT authentication settingsLogging- Log levels and providers
API Documentation
Scalar API Reference:- Accessible at
/scalar/v1in development - Interactive API testing
- Automatic endpoint discovery
- Request/response examples
- Schema documentation
Security Features
Authentication
- JWT Bearer token authentication
- Token-based stateless auth
- Configurable token lifetime
- Secure token signing with secret key
Authorization
- Role-based access control
[Authorize]attributes on controllers/actions- Custom authorization policies (if implemented)
Data Protection
- HTTPS enforcement
- Password hashing (recommended: use ASP.NET Core Identity)
- SQL injection prevention via EF Core
- Input validation
Error Handling
Standard Patterns:- Try-catch blocks in controller actions
ResponseAPIwrapper for consistent responses- HTTP status codes (200, 400, 401, 404, 500)
- Validation errors returned as BadRequest
Database Migrations
Entity Framework Core Migrations:- Code-first approach
- Migrations stored in
Migrations/folder - Version control for schema changes
Performance Considerations
Database Queries
- Async/await for all database operations
AsNoTracking()for read-only queries- Eager loading with
Include()where appropriate - Avoid N+1 query problems
Caching
- Consider response caching for static data
- Distributed caching for multi-instance deployments
Best Practices
API Design
- RESTful conventions
- Consistent endpoint naming
- Proper HTTP verbs (GET, POST, PUT, DELETE)
- Meaningful HTTP status codes
Code Organization
- Separate concerns (Controllers, Models, Services)
- Interface-based design
- Dependency injection
- Single Responsibility Principle
Security
- Never commit secrets to source control
- Use configuration providers (Azure Key Vault, etc.)
- Implement proper password hashing
- Validate all inputs
- Use parameterized queries (EF Core handles this)
Next Steps
- Database Architecture - Understanding the data model
- Frontend Architecture - How the frontend consumes the API
- Development Setup - Setting up the development environment