The form tag helper generates a URL based on the application's routing configuration and applies it to the form's action
attribute. It also generates a hidden field within the form containing the value of an anti-forgery token for request verification. If a method
attribute is not specified in the form element, the form tag helper will render one with a value of post
by default.
The form tag helper's primary role is to generate a URL for the form's action
attribute from the parameters passed to its custom attributes. Therefore, if you try to provide an action
attribute to the form tag helper in addition to the custom attributes, an exception will be raised.
Attribute | Description |
---|---|
action |
The name of the action method on an MVC controller |
all-route-data 1 |
Multiple route parameter values |
antiforgery 7 |
Specifies whether an anti-forgery token is rendered. |
area |
The name of the MVC area |
controller |
The name of the MVC controller |
fragment 2 |
The fragment in the URL |
host |
The domain |
page 3 |
The Razor page to link to |
page-handler 4 |
The Razor page handler method to invoke |
protocol |
The protocol (http, https, ftp etc) |
route 5 |
The name of the route |
route- 6 |
A single route parameter value |
Notes
If the target URL for the
action
includes multiple route parameters, their values can be packaged as aDictionary<string, string>
and passed to theall-route-data
parameter:@{ var d = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" } }; } <form asp-all-route-data="d">...</form>
If the route has parameters defined, the form tag helper will output the values as URL segments:
<form action="/Page/value1/value2">...</form>
. If it doesn't, the route parameters will be appended to the URL as query string values:<form action="/Page?key1=value1&key2=value2" method="post">...</form>
.The fragment is the value after a hash or pound sign (
#
) in a URL used to identify a named portion of a document. The "Notes" heading above has an identity value of "notes" and can be referenced in a URL using the anchor tag helper like this:<form asp-fragment="notes">...</form>
producing this HTML:
<form action="/Page#notes" method="post">...</form>
. It should be noted that fragments have no influence on the submission of a form.The name of the Razor page to link to must be provided without the file extension:
<form asp-page="page">...</form>
If no page name is specified, the tag helper will generate a link to the current page.
The page handler method name will appear as a query string value unless it has been included as a route parameter for the target page.
Razor pages doesn't support named routes. This parameter will only be used for MVC routes.
The
route-
parameter enables you to specify the value for a single route value. The route parameter name is added after the hyphen. Here, the route parameter name is "key1":
<form asp-route-key1="value1">...</form>
This renders as
<form action=/Page/value1" method="post">...</form>
if thekey1
parameter is defined as part of the page's route template, or
<form action="/Page?key1=value1" method="post">...</form>
if not.The anti-forgery token is rendered as a hidden input named
__RequestVerificationToken
. It will be rendered by default, unless the form has anaction
attribute specified, the form's method is set toGET
or theantiforgerytoken
value is set tofalse
. If you disable the generation of the anti-forgery token, you must also disable request verification on the PageModel class that houses the processing page handler.