Hi All ,
how to create layout in razor page in asp.net core ? From the Menu model get function with example .
can anyone suggest with example .
Thanks in advance
Hi All ,
how to create layout in razor page in asp.net core ? From the Menu model get function with example .
can anyone suggest with example .
Thanks in advance
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Deepika SawantRank #242 · 10.2K reputationPosted Jul 15, 2025, 12:21 PM EDT
1. Create the Layout Page (
_Layout.cshtml)Place this file in
Pages/Shared/_Layout.cshtml. It defines the common structure (header, footer, etc.) for your Razor Pages.2. Define the Menu Model
Create a simple model to represent menu items.
public class MenuItem
{
public string Text { get; set; }
public string Url { get; set; }
}
3. Create a Base PageModel to Share Menu Data
You can use inheritance to share menu data across pages.
public class LayoutModel : PageModel
{
public List
public override void OnPageHandlerExecuting(PageHandlerExecutingContext context)
{
MenuItems = new List
4. Use the Layout in Your Razor Page
In
Pages/Index.cshtml:@page
@model IndexModel
@{
Layout = "_Layout";
ViewData["Title"] = "Home Page";
}
Welcome to Razor Pages!
Use
_ViewStart.cshtmlto Apply Layout GloballyIn
Pages/_ViewStart.cshtml:@{
Layout = "_Layout";
}