Skip to content

Latest commit

 

History

History
256 lines (192 loc) · 6.45 KB

File metadata and controls

256 lines (192 loc) · 6.45 KB

Enterprise Web API

A comprehensive ASP.NET Core 7 Web API project demonstrating enterprise-level development practices with authentication, validation, and modern architectural patterns.

🌟 Features

  • ASP.NET Core Web API 7.0 - Latest framework version
  • Entity Framework Core - Database access with SQL Server
  • ASP.NET Core Identity - User management and authentication
  • JWT Bearer Authentication - Secure token-based authentication
  • AutoMapper - Object-to-object mapping
  • FluentValidation - Input validation with fluent syntax
  • Swagger/OpenAPI - API documentation and testing interface
  • Dependency Injection - Loosely coupled architecture
  • Repository Pattern - Data access abstraction
  • Service Layer - Business logic separation

🏗️ Project Structure

EnterpriseWebAPI/
├── Controllers/           # API controllers
│   ├── AuthController.cs  # Authentication endpoints
│   ├── ProductsController.cs # Product CRUD operations
│   └── WeatherForecastController.cs
├── Data/                  # Database context and configurations
│   └── ApplicationDbContext.cs
├── DTOs/                  # Data Transfer Objects
│   ├── ProductDtos.cs
│   └── UserDtos.cs
├── Models/                # Entity models
│   ├── Product.cs
│   └── User.cs
├── Services/              # Business logic services
│   ├── AuthService.cs
│   ├── IServices.cs
│   └── ProductService.cs
├── Validators/            # FluentValidation validators
│   ├── ProductValidators.cs
│   └── UserValidators.cs
├── Mapping/               # AutoMapper profiles
│   └── MappingProfile.cs
└── Program.cs             # Application configuration

🚀 Getting Started

Prerequisites

  • .NET 7.0 SDK
  • SQL Server (LocalDB for development)
  • Visual Studio 2022 or VS Code

Installation

  1. Clone the repository

    git clone <repository-url>
    cd EnterpriseWebAPI
  2. Restore packages

    dotnet restore
  3. Update database connection string

    Edit appsettings.json and appsettings.Development.json to configure your database connection.

  4. Run the application

    dotnet run
  5. Access Swagger UI

    Navigate to https://localhost:5001 or http://localhost:5000 to access the Swagger documentation.

📚 API Endpoints

Authentication

  • POST /api/auth/register - Register a new user
  • POST /api/auth/login - Login with email/password
  • GET /api/auth/me - Get current user information

Products

  • GET /api/products - Get all products
  • GET /api/products/{id} - Get product by ID
  • POST /api/products - Create new product (requires authentication)
  • PUT /api/products/{id} - Update product (requires authentication)
  • DELETE /api/products/{id} - Delete product (requires authentication)
  • GET /api/products/category/{category} - Get products by category
  • GET /api/products/search?searchTerm={term} - Search products

🔐 Authentication

The API uses JWT (JSON Web Tokens) for authentication. To access protected endpoints:

  1. Register a new user or login with existing credentials
  2. Include the JWT token in the Authorization header:
    Authorization: Bearer <your-jwt-token>
    

🛠️ Configuration

JWT Settings (appsettings.json)

{
  "JwtSettings": {
    "SecretKey": "YourSuperSecretKeyThatIsAtLeast32CharactersLong12345",
    "Issuer": "EnterpriseWebAPI",
    "Audience": "EnterpriseWebAPIUsers",
    "ExpirationInHours": 24
  }
}

Database Connection

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=EnterpriseWebAPI;Trusted_Connection=true;MultipleActiveResultSets=true"
  }
}

🏃‍♂️ Running the Application

Development

dotnet run --environment Development

Production

dotnet run --environment Production

Docker (Optional)

# Build the image
docker build -t enterprise-web-api .

# Run the container
docker run -p 8080:80 enterprise-web-api

🧪 Testing

Using Swagger UI

  1. Run the application
  2. Navigate to the Swagger UI (root URL)
  3. Use the "Authorize" button to input your JWT token
  4. Test the endpoints directly from the interface

Using Postman or cURL

Example registration request:

curl -X POST https://localhost:5001/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "firstName": "John",
    "lastName": "Doe",
    "email": "john.doe@example.com",
    "password": "Password123",
    "confirmPassword": "Password123"
  }'

📦 NuGet Packages

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore
  • Microsoft.AspNetCore.Authentication.JwtBearer
  • AutoMapper
  • AutoMapper.Extensions.Microsoft.DependencyInjection
  • FluentValidation
  • FluentValidation.AspNetCore
  • Swashbuckle.AspNetCore

🎯 Best Practices Implemented

  • Clean Architecture - Separation of concerns with layers
  • SOLID Principles - Dependency inversion, single responsibility
  • Async/Await - Non-blocking operations
  • Input Validation - FluentValidation for robust validation
  • Error Handling - Consistent error responses
  • Security - JWT authentication, password hashing
  • Documentation - Swagger/OpenAPI integration
  • Logging - Built-in .NET logging
  • Configuration - Environment-specific settings

🔧 Development Commands

# Add migration
dotnet ef migrations add InitialCreate

# Update database
dotnet ef database update

# Build project
dotnet build

# Run tests
dotnet test

# Publish for deployment
dotnet publish -c Release

📋 Environment Variables

For production deployment, consider using environment variables:

  • ConnectionStrings__DefaultConnection
  • JwtSettings__SecretKey
  • JwtSettings__Issuer
  • JwtSettings__Audience

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🆘 Support

For support and questions:

  • Create an issue in the repository
  • Contact the development team
  • Check the API documentation in Swagger UI

Enterprise Web API - Built with ❤️ using ASP.NET Core