This repository is an educational ASP.NET Core application designed to demonstrate foundational concepts in ASP.NET Core development. It serves as a boilerplate project for learning and experimenting with various ASP.NET Core features, including routing, dependency injection, middleware, custom attributes, and more.
This app is a basic API designed around a sample product and order management system. It uses a clean and modular structure to help illustrate best practices in ASP.NET Core, making it easy for developers to understand how different components fit together.
- Modular Project Structure: Organized folders for controllers, models, middleware, services, attributes, and configurations.
- Routing and Attribute-Based Routing: Demonstrates how to set up attribute-based routing in controllers and centralize route paths.
- Dependency Injection: Uses DI to inject services into controllers, promoting decoupled and testable code.
- Custom Middleware: Illustrates how to create and use custom middleware for pre- and post-processing of HTTP requests.
- Custom Attributes: Implements a custom attribute to add metadata (versioning) to controllers and actions.
- Configurations: Shows how to manage configurations and default settings in
appsettings.jsonand configuration classes. - Swagger Integration: Provides API documentation and testing through Swagger.
The project is structured in a modular and scalable way. Here’s an overview of the folder structure and its purpose:
MyApiProject/
├── Controllers/ # API controllers for handling HTTP requests
│ ├── ProductsController.cs
│ ├── OrdersController.cs
│
├── Models/ # Data models or entities representing resources
│ ├── Product.cs
│ ├── Order.cs
│
├── Middleware/ # Custom middleware components
│ ├── CustomMiddleware.cs
│
├── Routes/ # Centralized route configurations
│ ├── ApiRoutes.cs
│
├── Interfaces/ # Interfaces for services, supporting Dependency Injection
│ ├── IProductService.cs
│ ├── IOrderService.cs
│
├── Services/ # Business logic or service implementations
│ ├── ProductService.cs
│ ├── OrderService.cs
│
├── Attributes/ # Custom attributes for metadata or custom behavior
│ ├── VersionAttribute.cs
│
├── Configurations/ # Configuration classes mapping to appsettings.json
│ ├── DatabaseSettings.cs
│ ├── SwaggerSettings.cs
│
├── Defaults/ # Default values or settings for various entities
│ ├── ProductDefaults.cs
│
├── Data/ # Database-related classes (e.g., DbContext, seed data)
│ ├── AppDbContext.cs
│
├── Program.cs # Entry point of the application
├── Startup.cs # Configures services and middleware pipeline
├── appsettings.json # Application configuration
└── appsettings.Development.json # Development environment configuration
- Demonstrates the use of attribute-based routing with
[Route],[HttpGet],[HttpPost], etc. - Centralized routes are defined in
Routes/ApiRoutes.csto avoid hardcoding paths and improve maintainability.
- Registers services in
Startup.cs(e.g.,IProductServiceandProductService) and uses constructor injection to inject dependencies into controllers. - Promotes a decoupled architecture, allowing easy testing and flexibility in service implementation.
- Shows how to create and register custom middleware to handle request processing in the
Middleware/CustomMiddleware.csfile. - Middleware can be used for logging, authentication, response modifications, and more.
- Implements a custom
VersionAttributeto add metadata like version, author, and year to controllers or actions. - Demonstrates the use of attributes for extending class or method behavior, as well as reflection to read attribute data.
- Defines models in the
Modelsfolder to represent database entities, such asProductandOrder. Data/AppDbContext.csserves as the data access layer using Entity Framework Core, which connects to a database and performs CRUD operations.
- Stores configuration settings in
Configurations/DatabaseSettings.csand manages them inappsettings.json. - Default values, like
ProductDefaults, are organized in theDefaultsfolder to centralize constants.
- Integrates Swagger (via Swashbuckle) in
Startup.csto automatically generate API documentation and provide an interactive UI for testing endpoints.
To run the project, you need .NET SDK 6.0 or later.
-
Clone the repository:
git clone https://github.com/MichaelSel/HelloCSharp.git cd HelloCSharp -
Install dependencies and restore packages:
dotnet restore
-
Run the application:
dotnet run
-
Access the API documentation (Swagger):
- Go to
http://localhost:5000/swaggerto view and test the API.
- Go to
- .NET SDK 6.0 or later
- SQL Server (if using Entity Framework Core with SQL Server, configure the connection string in
appsettings.json)
This boilerplate app is designed to teach essential concepts in ASP.NET Core development:
- Organizing large-scale ASP.NET Core projects
- Implementing routing and managing API endpoints
- Using Dependency Injection to decouple components
- Extending application behavior with custom middleware and attributes
- Managing configurations and default settings
- Leveraging Swagger for API documentation
Feel free to modify and extend this project to experiment with new features or to adapt it as a starting point for your own ASP.NET Core projects.
This project is for educational purposes and is provided under the MIT License.