We know that you can place code and models in a. cshtml file or in a. cshtml.cs file that matches. cshtml.
The Razor page handler or processing method matches the user request to our method, requesting a file from the. cshtml.
The Razor page follows a specific naming convention. For example, the following:
- Onget
- Onpost
- Ongetasync
- Onpostasync
- Onpoststudent
From the example above we can see that the name starts with "on" and then follows its request, followed by the optional handler name, and finally the async end.
The default request handling method:
The default post request, and if you do not specify an action, it will look for the Onpost () method. If there is, then the corresponding method is called, and no method will be triggered.
<method= "POST"> <type= " Text "/></form>
Public void Onpost () { }
If you implement Onpost () and Onpostasync at the same time, the following error occurs:
If our page has multiple post requests, such as the following:
Public void Post () {} Public void Postfirst () {}
We can customize the request processing method by adding the Asp-page-handler property to the form form:
<method= "POST" asp-page-handler= "First"> <type= "text"/></form >
The above request will request the Postfirst () method, because the method's handler corresponds to the corresponding property of the form.
Processing method parameters
There are two ways to pass parameters to a processing method:
- Form input
- Form elements with Asp-route Tag Helper
Pass parameters via form input:
<method= "POST"> <type= " Text " name=" name "/></form>
Public void Onpost (string name) { }
Pass parameters by route:
The first request will request a Onpost method and assign a value of 666 to the parameter name.
The second request will request the Onpostremove method and assign a value of 1 to the parameter ID.
< form method = "POST" Asp-route-name = "666" > </ form > < form method = "post" asp-route-handler= "Remove" Asp-route-id = "1" > </ form >
Public void Onpost (string name) { }publicvoid onpostremove (int ID) {}
How to request an ASP. NET Core Razor page