A clean, production-style ASP.NET Core 8 Razor Pages web application demonstrating how to integrate MongoDB as the primary data store — featuring a generic repository/service pattern, MongoDB transactions with sessions, and a complete CRUD user management module.
- Overview
- Features
- Tech Stack
- Architecture
- Project Structure
- Getting Started
- Application Pages
- How It Works
- Extending the Project
- License
- Author
AspProMongoDb shows a practical, layered approach for building web apps on top of MongoDB with .NET. Instead of scattering database calls across page models, all data access flows through a generic base service (BaseServices<TEntity>) that wraps the official MongoDB C# driver, wires every write operation into a client session + transaction, and resolves collections automatically by entity name.
The included sample module is a User Management system with full Create / Read / Update / Delete pages built with Razor Pages and Bootstrap 5.
- ✅ Full CRUD for users (list, create, view details, edit, delete)
- 🧱 Generic Repository/Service pattern — add a new entity with just a few lines of code
- 🔐 MongoDB Transactions — inserts, updates, and deletes run inside a session-backed transaction
- 🆔 GUID primary keys with standard BSON GUID representation (
BaseEntity) - 💉 Built-in Dependency Injection —
IMongoClient(singleton),MongoDbContext(scoped), services (transient) - ⚙️ Configuration-driven — connection string & database name live in
appsettings.json(Options pattern) - 🎨 Responsive UI — Bootstrap 5, jQuery validation, RTL-ready assets included
| Layer | Technology |
|---|---|
| Framework | .NET 8 / ASP.NET Core Razor Pages |
| Language | C# 12 (primary constructors, implicit usings, nullable enabled) |
| Database | MongoDB (official MongoDB.Driver v3.10.0) |
| Frontend | Razor (CSHTML), Bootstrap 5, jQuery + jQuery Validation |
| IDE | Visual Studio 2022 (solution: AspProMongoDb.sln) |
┌─────────────────────────────────────────────────────┐
│ Razor Pages (UI) │
│ Pages/Users → Index · Create · Edit · Details · │
│ Delete │
└───────────────────────┬─────────────────────────────┘
│ IUserServices (DI)
┌───────────────────────▼─────────────────────────────┐
│ Service Layer │
│ UserServices : BaseServices<User> │
│ Generic CRUD: Insert / Update / Delete / │
│ GetById / GetAll │
└───────────────────────┬─────────────────────────────┘
│ MongoDbContext
┌───────────────────────▼─────────────────────────────┐
│ Data Access Layer │
│ IMongoClient → Session → Transaction → Commit │
└───────────────────────┬─────────────────────────────┘
│
┌───────▼────────┐
│ MongoDB │
│ (Replica Set) │
└────────────────┘
MongoDb_Project/
├── AspProMongoDb.sln # Visual Studio solution
└── AspProMongoDb.web/ # Main web application
├── Comm/ # Common/shared abstractions
│ ├── BaseEntity.cs # Base entity with GUID BsonId
│ ├── IBaseServices.cs # Generic CRUD contract
│ └── BaseServices.cs # Generic CRUD implementation (transactional)
├── DataBase/
│ └── MongoDbContext.cs # Session, database & transaction management
├── Entities/
│ └── User.cs # User entity (FullName, UserName, Email)
├── Model/
│ └── MongoSettings.cs # Strongly-typed Mongo configuration
├── Services/
│ ├── IUserServices.cs # User service contract
│ └── UserServices.cs # User service implementation
├── Pages/
│ ├── Users/ # CRUD pages for users
│ │ ├── Index.cshtml # List all users
│ │ ├── Create.cshtml # Add a new user
│ │ ├── Details.cshtml # View a user
│ │ ├── Edit.cshtml # Update a user
│ │ └── Delete.cshtml # Remove a user
│ ├── Shared/ # Layout & partials
│ ├── Index.cshtml # Home page
│ └── Privacy.cshtml
├── wwwroot/ # Static assets (Bootstrap, jQuery, CSS, JS)
├── appsettings.json # App configuration (Mongo connection)
└── Program.cs # App bootstrap & DI registration
- .NET 8 SDK
- MongoDB Community Server running as a replica set (required for transactions)
MongoDB transactions only work on a replica set. For local development, a single-node replica set is enough:
-
Start
mongodwith a replica set name:mongod --replSet rs0 --dbpath <your-data-path>
Or add this to your
mongod.cfgand restart the MongoDB service:replication: replSetName: rs0
-
Initialize the replica set (one time only), using
mongosh:rs.initiate()
Connection settings are in AspProMongoDb.web/appsettings.json:
{
"MongoSettings": {
"ConnectionString": "mongodb://localhost:27017/?replicaSet=rs0",
"DataBaseName": "AspPro_Mongo"
}
}Adjust the connection string / database name to match your environment. The database and the User collection are created automatically on first write.
git clone https://github.com/MohammadHasanp/MongoDb_Project.git
cd MongoDb_Project
dotnet restore
dotnet run --project AspProMongoDb.webThen open your browser at:
- HTTP →
http://localhost:5108 - HTTPS →
https://localhost:7030
Navigate to /Users to try the CRUD module.
| Route | Page | Description |
|---|---|---|
/ |
Home | Landing page |
/Users |
Users → Index | List of all users |
/Users/Create |
Users → Create | Add a new user |
/Users/Details |
Users → Details | View a single user by id |
/Users/Edit |
Users → Edit | Update an existing user |
/Users/Delete |
Users → Delete | Confirm & delete a user |
- DI Registration (
Program.cs) —MongoSettingsis bound from configuration,IMongoClientis registered as a singleton,MongoDbContextas scoped, andIUserServicesas transient. MongoDbContext— starts a client session on construction and exposesStartTransaction()/Commit()plus access to the configured database.BaseServices<TEntity>— resolves the collection by entity type name (e.g.User→Usercollection) and performs CRUD; every write (Insert,Update,Delete) is wrapped in a transaction.- Razor Page Models — receive
IUserServicesvia constructor injection (C# primary constructors) and stay completely free of database details.
Adding a new entity takes three small steps:
// 1. Create the entity
public class Product : BaseEntity
{
public string Name { get; set; }
public decimal Price { get; set; }
}
// 2. Create the service contract + implementation
public interface IProductServices : IBaseServices<Product> { }
public class ProductServices(MongoDbContext context)
: BaseServices<Product>(context), IProductServices;
// 3. Register it in Program.cs
services.AddTransient<IProductServices, ProductServices>();That's it — you instantly get transactional Insert, Update, Delete, GetById, and GetAll for the new entity.
Mohammad Hasan Pirayandeh — @MohammadHasanp
⭐ If you find this project useful, please give it a star!