ASP.NET Core Razor Pages & Views 1 — Questions and Answers
Question 1: What attribute automatically binds a Razor Page's PageModel property to incoming HTTP POST form data?
- [BindProperty] (Correct answer)
- [FromForm]
- [ModelBind]
- [DataMember]
Correct answer: [BindProperty]
[BindProperty] is the dedicated attribute for binding PageModel properties to form data on POST requests in Razor Pages.
Question 2: Which handler method signature handles HTTP GET requests in a Razor Pages PageModel?
- OnPost()
- OnGet()
- OnGetAsync()
- Both OnGet() and OnGetAsync() are valid (Correct answer)
Correct answer: Both OnGet() and OnGetAsync() are valid
Both synchronous OnGet() and asynchronous OnGetAsync() are valid handler methods for GET requests in Razor Pages.
Question 3: What Razor directive declares the model type for a Razor view, enabling IntelliSense and strong typing?
- @using
- @inherits
- @model (Correct answer)
- @inject
Correct answer: @model
The @model directive declares the C# type of the Model property available in the Razor view, enabling strong typing.
Question 4: Where are Razor Pages .cshtml files stored by default in an ASP.NET Core project?
- /Controllers
- /Views
- /Pages (Correct answer)
- /Razor
Correct answer: /Pages
By convention, Razor Pages files are stored in the /Pages folder, and the folder structure maps directly to URL routes.
Question 5: What is the file extension for a Razor Page's code-behind PageModel file?
- .cs
- .cshtml.cs (Correct answer)
- .razor.cs
- .page.cs
Correct answer: .cshtml.cs
The PageModel code-behind uses the .cshtml.cs extension, pairing it with the corresponding .cshtml view file.
Question 6: Which method should a PageModel return to redirect the user to a different Razor Page after processing a POST?
- return View()
- return RedirectToPage() (Correct answer)
- return RedirectToAction()
- return Navigate()
Correct answer: return RedirectToPage()
RedirectToPage() is the Razor Pages method for redirecting to another page, resolving routes relative to the Pages folder.
Question 7: What does the asp-page Tag Helper attribute do when applied to an anchor tag in a Razor view?
- Specifies the CSS class for the anchor
- Generates an href URL pointing to the specified Razor Page (Correct answer)
- Loads a partial view from the Pages folder
- Sets the page title in the HTML head
Correct answer: Generates an href URL pointing to the specified Razor Page
The asp-page tag helper generates the correct href URL for the specified Razor Page using ASP.NET Core's routing system.
What attribute automatically binds a Razor Page's PageModel property to incoming HTTP POST form data?