Complete C# Tutorial

Master Page Layouts in ASP.NET Core – The Beginner-Friendly Guide

🌟 Introduction

If you want your ASP.NET Core website to look clean, consistent, and professional, then Master Page Layouts are your best friend! They save time, reduce duplicate code, and make your project super easy to maintain.

In this guide, you will master the concept of .Net Core Master Page and Master Page layout in ASP.NET, even if you are just starting. I will walk you through everything in a friendly, simple, and fun way. ✨

🏗️ What is a Master Page Layout in ASP.NET Core?

A Master Page Layout in ASP.NET (also called _Layout.cshtml) is like a template for your website pages.

It defines the header, footer, menu, scripts, and overall design, so every page looks similar.
This is why developers use the .Net Core Master Page — it saves time and avoids repetition.

Think of it like:
👉 One design → used by many pages.
👉 Change once → update everywhere automatically.

📁 Default Layout File in ASP.NET Core

In every MVC project, the default layout is here:

/Views/Shared/_Layout.cshtml

This file contains:

  1. HTML <head> section
  2. Navigation bar
  3. Footer
  4. Script references
  5. @RenderBody() where page content is injected

💻 Code Example 1 — Simple Master Page Layout

📄 _Layout.cshtml

				
					<!DOCTYPE html>
<html>
<head>
    <title>@ViewData["Title"] - MySite</title>
</head>
<body>
    <header>
        <h1>Welcome to My Website</h1>
        <hr />
    </header>

    <div>
        @RenderBody()
    </div>

    <footer>
        <hr />
        <p>© 2025 MySite</p>
    </footer>
</body>
</html>
				
			

📄 Index.cshtml

				
					@{
    ViewData["Title"] = "Home";
    Layout = "_Layout";
}

<h2>This is the Home Page Content.</h2>
				
			

🖥 Expected Output

				
					Welcome to My Website
---------------------------------------

This is the Home Page Content.

---------------------------------------
© 2025 MySite
				
			

💡 Explanation

  1. Layout = "_Layout" connects your view to the master page.
  2. @RenderBody() acts like a placeholder where each page injects its own content.
  3. You change header/footer once, and every page automatically updates.

This is the power of .Net Core Master Page and Master Page layout in ASP.NET.

💻 Code Example 2 — Adding a Navigation Menu

📄 _Layout.cshtml

				
					<body>
    <nav>
        <a href="/Home">Home</a> |
        <a href="/About">About</a> |
        <a href="/Contact">Contact</a>
    </nav>

    <hr />
    @RenderBody()
</body>
				
			

🖥 Expected Output

				
					Home | About | Contact
----------------------------------
[Your page content here]
				
			

✔️ Now every page gets the same menu automatically.

💻 Code Example 3 — Using RenderSection

Sometimes you want a page to insert custom scripts or styles.

📄Layout

				
					@RenderSection("ExtraScripts", required: false)
				
			

View Page:

				
					@section ExtraScripts {
    <script>alert("Hello from this page!");</script>
}
				
			

🖥 Output

A unique script runs only for that page.

🎯 Real-Life Scenario

Imagine you are building an E-commerce website with

  • 100 product pages
  • 20 category pages
  • 10 static pages

Without .Net Core Master Page, you would manually copy:

❌ Header
❌ Menu
❌ Footer
❌ CSS/JS links

…into EVERY page. 😵

But with Master Page layout in ASP.NET:

✔️ You only design once
✔️ All 130+ pages use it
✔️ You update at one place → Reflects everywhere
✔️ Saves HOURS of work
✔️ Zero repeated code

Super useful during redesigns or quick fixes!

👉 Next what?

In the next chapter you will learn “.Net Core Master Page”

(Topic: Partial Views – Reusable UI Blocks)

Get ready — this is going to make your UI development even faster! 🚀

Leave a Comment

14 + 5 =

Share this Doc

Layouts (Master Pages)

Or copy link