How to request an ASP. NET Core Razor page

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.