Development
1. Create a web app and put it under version control
Overview In this exercise you will create a new .Net MVC web application using the dotnet CLI “scaffolding” tool and put the code under version control in a local git repository Step 1. Git Configuration Open your terminal. Configure your Git username and email: git config --global user.name "Your Name" git config --global user.email "your_email@example.com" Replace "Your Name" and "your_email@example.com" with your actual name and email1 address. This information will be associated with your commits. »
2. Intro to MVC
Create your first Model View Controller in an ASP.NET MVC Application Goal To implement a basic newsletter feature in an ASP.NET MVC application with functionality to display a subscriber’s information. Step-by-step Guide 1. Create the Model The model represents the data structure used in your application. Steps: Navigate to the Models folder in your project. Add a new class file named Subscriber.cs. Define the following properties in the Subscriber class: Models/Subscriber. »
3. Create a Form with Validation and Feedback
Goal Enhance your ASP.NET MVC application by creating a subscription form that validates user input on the client side. Follow these steps to implement client-side validation and provide immediate feedback to users. Step-by-step Guide 1. Update Your Model The model defines the data structure for your application. In this step, you’ll add validation attributes to enforce rules for the Name and Email fields. Steps: Open the Subscriber.cs file in the Models folder. »
4. Add Basic Authentication
Goal In this tutorial, we’ll add cookie-based authentication to your ASP.NET MVC application. This allows users to log in, access restricted pages, and log out. Step-by-step Guide 1. Add Authentication Services in Program.cs To enable cookie-based authentication, you need to configure the authentication middleware in your application. Steps: Open the Program.cs file in the root of your project. Add the AddAuthentication and AddCookie services to the builder.Services configuration. Program.cs ... // Add support for basic authentication builder. »
5. Introduce a service layer
Goal Enhance your ASP.NET MVC application by introducing a service layer that handle business logic. We will perform server-side validation and simulate to store the subscriber into a database. This approach separates the presentation layer, which handles user interaction, from the business logic layer, which enforces rules and processes core application use cases. Step-by-step Guide 1. Create a ValidationResult Class The ValidationResult class will encapsulate the outcome of validation, including success status and error messages. »
6. Use the Repository Pattern (In-Memory DB)
Goal In this tutorial, we will add support for an in-memory database to store subscribers using the repository pattern. The repository will act as an abstraction layer to manage data storage and retrieval. We will also update the service layer to utilize the repository, replacing the current mocked list. Step-by-step Guide 1. Define the ISubscriberRepository Interface The ISubscriberRepository interface defines the contract for interacting with subscriber data. This includes methods for adding, retrieving, and managing subscribers. »
7. List Subscribers (only logged in users)
Goal In this tutorial, you will learn how to: List all subscribers on a protected page. Restrict access to the subscriber list so only logged-in users can view it. Add an unsubscribe button for each subscriber in the list. Display a success message when a user is successfully unsubscribed. Add a Subscribers link in the navigation bar that toggles visibility based on the login status. Step-by-step Guide 1. Update the Newsletter Service We need to add methods to fetch all subscribers and to unsubscribe a specific subscriber by their ID. »
8. Create a Docker Compose File for MongoDB
Goal In this tutorial, you will learn how to: Create a docker-compose.yaml file to run MongoDB in a container. Add Mongo Express to visually manage the MongoDB database through a web interface. Configure persistent storage for MongoDB data. Connect your ASP.NET application to the MongoDB instance running in Docker. Verify that the application correctly interacts with MongoDB. Step-by-step Guide 1. Create the Docker Compose File We’ll use Docker Compose to define and manage multi-container applications. »
9. Add Repository for MongoDB
Goal In this tutorial, you will learn how to: Add the MongoDB driver package to your ASP.NET project. Configure MongoDB connection settings in appsettings.json. Create a MongoDbSettings class to manage MongoDB configuration. Implement the MongoDbSubscriberRepository to handle CRUD operations. Register the MongoDB repository in Program.cs for dependency injection. Step-by-step Guide 1. Add the MongoDB Driver Package We need to add the official MongoDB driver to our project to interact with MongoDB. »