Chapter 2
MVC in Action
01 Prepare Development Machines 04 Middleware
02 MVC Demo 05 SQL Management Studio
03 Dependency Injection 06 Share Learning Resources
Demo: 1st Visual Studio Project
Create a Basic ASP .NET Core (MVC) Project
01 Create New Project
02 Add Authentication
03 Understand Structure and Files
04 Update Packages
www.usal.edu.lb
Demo: Create Project
Create a Basic ASP .NET Core (MVC) Project
www.usal.edu.lb
Demo: Create Project
Search for Asp.net core web app
Select Asp.net core Web App
(Model-View-Controller)
Click Next
www.usal.edu.lb
Demo: Create Project
Enter the Project Name and Location
www.usal.edu.lb
Demo: Create Project
• Make sure to Select 8.0 (LTS)
• Use Individual Accounts as Authentication
www.usal.edu.lb
Demo: Scaffold Identity Pages
Scaffolding Identity Pages allows us to override
the existing Identity Pages such as Login,
Logout, Reset Passwords, etc.
• Right Click on the Project in Solution
Expolorer -> Add -> New Scaffolded Item
www.usal.edu.lb
Demo: Scaffold Identity Pages
• Select Identity • Select Pages you want to Scaffold
• Set the DbContext
• Click Add
www.usal.edu.lb
Demo: Solution Explorer
Our Static Files Scaffolded Pages Related to Identity
Controllers Folder
Migration Classes relation to code-first Approach
DbContext Related to Identity
Models Folder
Views Folder
Program.cs
www.usal.edu.lb
Demo: Update Client-side Libraries
To Update Client-Side Libraries:
- Delete existing folder
- Right click on wwwroot folder
- Add -> Client Side Library
www.usal.edu.lb
Demo: Update Client-side Libraries
To Update Client-Side Libraries: (Continued)
- Enter package name
- Click Install
www.usal.edu.lb
Demo: Update NuGet Packages
To Update NuGet Packages: Right Click on Project File, and Select Manage NuGet Packages
www.usal.edu.lb
Dependency Injection
What is Dependency Injection?
Dependency Injection (DI) is a design pattern used to manage dependencies in software development.
It helps to achieve loose coupling and improved maintainability.
A core concept in frameworks like ASP.NET Core, Spring, Angular, etc.
Add Services to DI Container (Program.cs)
builder.Services.AddScoped<IDatabaseService, DatabaseService>();
builder.Services.AddTransient<UserService>();
www.usual.edu.lb 24
Dependency Injection
Consume Services
public class UserService
{
private readonly IDatabaseService _databaseService;
public UserService(IDatabaseService databaseService)
{
_databaseService = databaseService; // Injected dependency
}
public void GetUser()
{
_databaseService.FetchUser();
}
}
www.usual.edu.lb 25
Middlewares
What is Middleware?
Middleware is software that processes requests and
responses in a web application.
Acts as a pipeline where each middleware can
modify, handle, or pass requests further.
Commonly used in frameworks like ASP.NET Core,
Express.js, and Django.
www.usual.edu.lb 26
Middlewares
Add Middleware to ASP.net Core request pipeline in Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// Start Adding Middlewares
app.UseRouting(); // Middleware for route processing
app.UseAuthentication(); // Authentication Middleware
app.UseAuthorization(); // Authorization Middleware
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run();
www.usual.edu.lb 27
Middlewares
Middleware Common Examples
• Authentication & Authorization – Ensures only authorized users access resources.
• Logging & Monitoring – Tracks requests and responses for debugging.
• Exception Handling – Manages application errors.
• Caching – Improves performance by storing responses.
• Compression – Reduces response size for faster load time
You can build and use your custom middleware.
For more Info about middlewares in Asp.net Core
www.usual.edu.lb 28
Demo: SQL Management Studio
It is very useful if you want to follow Database-First Approach
01 Create New Diagram
02 Create Tables and Relations
03 Generate Scripts
www.usal.edu.lb
Demo: Connect to SQL Instance
• Open the SQL Management studio
• Select the Authentication type
• Windows Authentication
• SQL Server Authentication
• Connect
www.usal.edu.lb
Demo: Create Empty Database
• Right Click on Databases
• Click New Database
1 • Enter Database Name
• Click Ok
2
www.usal.edu.lb
Demo: Create Diagram
• Expand the Database Node • This Dialog allows us to add “Existing”
• Right click on the “Database Diagrams” and click “New tables to our Diagram
Database Diagram”
• Press Yes if Any Prompt Shown
www.usal.edu.lb
Demo: Create Tables and Relations
• Set a Column as Primary Key in the
Context Menu
• Right Click on the Diagram Area and Select “New Table”
• Enter Table Name
• Specify Tables Columns and Data Type
www.usal.edu.lb
Demo: Create Tables and Relations
• Open the Column Properties and Set “Is Identity” as “Yes”,
www.usal.edu.lb
Demo: Create Tables and Relations
• In this example, to make the
Course.StudentID as Foreign Key, Drag It
from the Course Table to the Student
Table.
• Click Ok on the Dialog
www.usal.edu.lb
Demo: Generate Change Script
• Click “Generate Change Script” to get
the SQL Scripts for Changes applied in
the diagram including the
• Table Creation
• Table Modification
• Relations
• Primary Keys and Foreign Keys
www.usal.edu.lb
Learning Intro to Visual Studio C# for Beginners
Resources
Learning
Resources Microsoft Learn Website
Discussion