ASP.NET Nano Web API
What Is an ASP.NET Core Web API?
Um API Web do ASP.NET Core provides HTTP endpoints that allow frontend applications and external clients to interact with a backend. In a clean architecture solution, the Web API project typically serves as the entry point for HTTP requests, hosts API controllers, configures middleware, and brings together the application’s services and dependencies.
In Nano ASP.NET Boilerplate, the WebApi project is the main entry point for applications using a React or Vue single-page application (SPA). It contains Program.cs, application configuration, middleware, and REST API controllers. The other layers, including Application, Domain, and Infrastructure, provide the underlying business logic and data access functionality.
If you use Razor Pages instead of a separate SPA, the RazorApp project serves as the presentation layer. See the Razor Pages overview.
Service Registration
ASP.NET Core uses dependency injection (DI) to manage application services and their dependencies. Nano organizes service registration into extension methods, keeping Program.cs focused on application startup rather than filling it with individual registrations.
In Nano, the ConfigureApplicationServices method in ServiceCollectionExtensions coordinates the registration of application services and infrastructure.
ServiceCollectionExtensions
The static ServiceCollectionExtensions class contains the ConfigureApplicationServices extension method. It registers services that require explicit configuration, including:
- Cross-Origin Resource Sharing (CORS)
- FluentValidation
- AutoMapper
- MailKit
- Cloudinary
- API endpoints
- Entity Framework Core database contexts
- Identidade do ASP.NET Core
- Authentication
- Rate limiting, with three predefined policies:
RateLimitingPolicyHigh,RateLimitingPolicyMedium, eRateLimitingPolicyLow
Rate Limiting
Nano registers three rate-limiting policies that control how frequently clients can access specific endpoints. The predefined policies provide different rate limits and can be adjusted as needed to suit your application’s requirements.
Apply a policy to an individual controller endpoint using the [EnableRateLimiting] attribute. Rate limiting is particularly useful for endpoints decorated with [AllowAnonymous], such as login, registration, and password-reset endpoints, which may be exposed to unauthenticated requests.
For example, to apply the high-rate-limiting policy to an endpoint:
[AllowAnonymous]
[EnableRateLimiting("RateLimitingPolicyHigh")]
[HttpPost("login")]
public async Task<IActionResult> Login(LoginRequest request)
{
// Login logic
}
Replace RateLimitingPolicyHigh com RateLimitingPolicyMedium ou RateLimitingPolicyLow as appropriate. Choose the policy based on the endpoint’s purpose and expected usage, and adjust the configured limits to meet your security and performance requirements.
After registering the database contexts, Nano runs AddAndMigrateTenantDatabases to apply pending migrations. In the single-database multi-tenancy configuration, the corresponding method is named AddAndMigrateDatabase. In a multi-database setup, migrations are applied to the primary database and the tenant databases.
Services that do not require this explicit configuration are registered through the AddServices method in DynamicServiceRegistrationExtensions.
DynamicServiceRegistrationExtensions
O DynamicServiceRegistrationExtensions class provides the AddServices method, which automatically registers classes implementing Nano’s ITransientService ou IScopedService marker interfaces.
This convention reduces manual dependency-injection configuration when you add new services.
For example, Produto e Serviço implements IProductService, which inherits from IScopedService. Nano therefore registers Produto e Serviço with a scoped lifetime, meaning an instance is typically shared within a single request scope. Services that implement ITransientService are registered with a transient lifetime.
O AddServices method is called from ConfigureApplicationServices, bringing the application’s service registration logic together.
App Settings and Configuration
O appsettings.json file contains application configuration, including settings such as the database connection string, logging levels, and host-related options.
Update these settings to match your development and deployment environments. For production deployments, sensitive values such as connection strings and API keys should be supplied through environment variables or an appropriate secrets-management mechanism rather than committed to source control.
Nano includes test credentials and configuration for Ethereal Email e Cloudinary. Replace these with your own service credentials before using the application in a production environment.
Static Files and wwwroot
O wwwroot directory contains static files served by the application.
If you’re using the React frontend ou Vue frontend, place the compiled frontend build files in this directory for deployment.
In the SPA configuration, the FallbackController directs frontend navigation requests to index.html em wwwroot. This allows the client-side application to handle routes that are not API endpoints.
Middleware and Tenant Resolution
ASP.NET Core middleware processes HTTP requests before they reach a controller or endpoint. Nano uses middleware such as TenantResolver to identify tenant and user context for requests.
When a request includes a valid JWT bearer token, TenantResolver obtains the tenant and user identifiers from the authenticated request context. These values are made available through CurrentTenantUserService, allowing application services to apply tenant isolation and user-specific behavior.
Tenant resolution and authentication work together to support Nano’s multi-tenant architecture. For more information, see the Authentication and Authorization guide e o Multi-Tenancy guide.
API Controllers
API controllers expose HTTP endpoints for frontend applications and other clients. In Nano, controllers in the WebApi project delegate application operations to services in the Application and Infrastructure layers, helping keep HTTP handling separate from business logic and data access.
You can protect controllers or individual endpoints with the [Authorize] attribute. Role-based authorization can further restrict access to users with the appropriate permissions. See the Authentication and Authorization guide for details.
Scaffolding API Controllers
Nano includes a .NET CLI template for generating new API controllers and CRUD functionality. Use the dotnet new nano-controller command to scaffold a controller for a new entity or resource.
Refer to the Nano CLI tool guide for available parameters and examples.
Próximos passos
Next, explore Serviços de Aplicativos to see how Nano organizes business logic by examining the example Produto e Serviço.