The asp-page attribute is a built-in ASP.NET Core tag helper attribute for selecting a Razor Page as a generated navigation or form-submission target. The routing system converts the page name into the rendered href or action URL, so route configuration changes are reflected automatically. section 26.3.1 section 27.3.1
Use asp-page with the surrounding tag-helper attributes when the target is a Razor Page:
- Set asp-page on an anchor to generate its href, using a page path such as /suppliers/list. section 26.3.1
- Set asp-page on a form to generate its action URL; specify method explicitly when the form should submit with POST. section 27.3.1
- Use asp-page-handler to select a handler method and asp-route-* attributes to supply route values. section 26.3.1 section 27.3.1
- Apply asp-page to a button outside its form when the button uses form to identify the form being submitted. section 27.3.2
Targeting a Razor Page from a form
A form such as <form asp-page="FormHandler" method="post"> is rendered with an action targeting /pages/form. The same attribute can be placed on an external button, where the tag helper generates the button's formaction. section 27.3.1 section 27.3.2
Listing 27.11 Setting the form target in the FormHandler.cshtml file in the Pages folder
...
<div class="m-2">
<h5 class="bg-primary text-white text-center p-2">HTML Form</h5>
<form asp-page="FormHandler" method="post">
<div class="form-group">
<label>Name</label>
<input class="form-control" name="Name"
value="@Model.Product?.Name" />
</div>
<button type="submit" class="btn btn-primary mt-2">Submit</button>
</form>
</div>
...
Inspect a complete Razor Page form using asp-page, an explicit POST method, model-bound input, and a submit button. section 27.3.1
