ASP.NET Core Razor Pages & Views 2 — Questions and Answers
Question 1: What is the primary purpose of a Razor Layout page in ASP.NET Core?
- To define shared HTML structure like headers and footers reused across multiple views (Correct answer)
- To define the database schema for Entity Framework
- To configure middleware pipeline ordering
- To manage CSS grid layouts for responsive design
Correct answer: To define shared HTML structure like headers and footers reused across multiple views
Layout pages define a common HTML structure (headers, footers, navigation) that multiple content views can share, eliminating repetition.
Question 2: Which file is conventionally used to set a default layout for all Razor views in a folder and its subfolders?
- _ViewImports.cshtml
- _ViewStart.cshtml (Correct answer)
- _Layout.cshtml
- _Default.cshtml
Correct answer: _ViewStart.cshtml
_ViewStart.cshtml executes before any view in the same folder and is typically used to set the Layout property globally.
Question 3: What does calling @RenderBody() inside a Razor Layout page accomplish?
- It renders the HTML <body> opening tag
- It inserts the content page's HTML at that location within the layout (Correct answer)
- It loads a named partial view
- It renders the navigation menu component
Correct answer: It inserts the content page's HTML at that location within the layout
@RenderBody() is the placeholder in a layout where the child content page's unique HTML markup is rendered.
Question 4: How do named sections work between a layout page and a content page?
- Both the layout and content page use @RenderSection()
- Both the layout and content page use @section { }
- @RenderSection() in the layout renders a section defined with @section in the content page (Correct answer)
- @section in the layout renders content defined with @RenderSection() in the content page
Correct answer: @RenderSection() in the layout renders a section defined with @section in the content page
@RenderSection('Name') in the layout defines where the section renders; @section Name { } in the content page supplies the HTML for that section.
Question 5: What is the purpose of _ViewImports.cshtml in an ASP.NET Core project?
- To import external CSS stylesheets into views
- To share @using directives and @addTagHelper registrations across all views in a folder (Correct answer)
- To import JavaScript modules for Razor views
- To configure output caching for views
Correct answer: To share @using directives and @addTagHelper registrations across all views in a folder
_ViewImports.cshtml provides shared @using, @addTagHelper, and other directives to all views in its folder and subfolders, avoiding repetition.
Question 6: Which mechanisms can pass data from a Razor Pages PageModel to its view beyond the strongly-typed Model property?
- ViewData only
- TempData only
- ViewBag only
- ViewData, ViewBag, and TempData are all valid options (Correct answer)
Correct answer: ViewData, ViewBag, and TempData are all valid options
ViewData (dictionary), ViewBag (dynamic wrapper over ViewData), and TempData all provide ways to pass data from a PageModel to its view.
Question 7: When using a standard HTML <form method='post'> in a Razor Page without any asp-page attribute, where does the form submit to by default?
- The site root (/)
- The current Razor Page's OnPost handler (Correct answer)
- A generic form processing endpoint
- It throws a routing error without asp-page specified
Correct answer: The current Razor Page's OnPost handler
In Razor Pages, a form with method='post' automatically targets the current page's OnPost handler method by default.
What is the primary purpose of a Razor Layout page in ASP.NET Core?