liveBook · Manning

Form tag helper

As explained in Pro ASP.NET Core 7, Tenth Edition

FormTagHelper is an ASP.NET Core tag helper that processes form elements in Razor markup and generates form-related attributes and antiforgery content.

FormTagHelper processes form elements in Razor markup, using ASP.NET Core routing to generate action URLs for controller actions or Razor Pages instead of hard-coded paths. section 27.3.1

Configure the form target with attributes:

  • Use asp-action to select a controller action; omit asp-controller to target the controller rendering the view. section 27.3.1
  • Use asp-page to target a Razor Page and asp-page-handler to select its handler method. section 27.3.1
  • Specify method explicitly: without it, the helper adds method="post", which can surprise developers expecting HTML GET behavior. section 27.3.1
  • When the form target is generated through routing attributes, the helper adds an anti-CSRF security token to the response. section 27.8

Target a controller action

<form asp-action="submitform" method="post"> <input name="Name" value="@Model.Name" /> <button type="submit">Submit</button> </form> This renders a form whose action targets the routed submitform action, such as /controllers/Form/submitform. A Razor Page form can instead use asp-page="FormHandler" and render an action such as /pages/form.

Table 27.3 The built-in tag helper attributes for form elements (view table figure)

Name

Description

asp-controller

This attribute is used to specify the controller value to the routing system for the action attribute URL. If omitted, then the controller rendering the view will be used.

asp-action

This attribute is used to specify the action method for the action value to the routing system for the action attribute URL. If omitted, then the action rendering the view will be used.

asp-page

This attribute is used to specify the name of a Razor Page.

asp-page-handler

This attribute is used to specify the name of the handler method that will be used to process the request. You can see an example of this attribute in the SportsStore application in chapter 9.

asp-route-*

Attributes whose name begins with asp-route- are used to specify additional values for the action attribute URL so that the asp-route-id attribute is used to provide a value for the id segment to the routing system.

asp-route

This attribute is used to specify the name of the route that will be used to generate the URL for the action attribute.

asp-antiforgery

This attribute controls whether anti-forgery information is added to the view, as described in the “Using the Anti-forgery Feature” section.

asp-fragment

This attribute specifies a fragment for the generated URL.

Browse the complete attribute table for controller, action, Razor Page, handler, and additional-route-value configuration. section 27.3.1

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains attribute configuration, rendered URLs, POST defaults, and anti-CSRF behavior without detailing helper internals.
Examples
Uses controller and Razor Page forms, rendered action URLs, submitted form data, and the _RequestVerificationToken field.
What you can do after
Apply it via Razor tag-helper attributes to build forms that follow routing changes and include anti-CSRF protection.
How it connects
Connects Razor markup with ASP.NET Core routing, controller actions, Razor Pages, page handlers, and anti-cross-site request forgery.

Also explained in

Compare how 2 books explain form tag helper →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.