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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Deepika SawantPosted Jul 15, 2025, 4:21 PM
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";
}