In fact, the request method we usually encounter is nothing more than get/post, but many web developers still cannot tell the two.
Get is the way to pass the value to the server through querystring in the URL. Its data is visible, and post is a value to the server through a postdata package, the post method can transmit more data (such as uploading files), and is more secure (such as logon ).
This document describes how to submit a request to the Controller through the client page (that is, the final page generated) and how to accept the request in the controller.
Conventions
All the examples in this article will demonstrate a logon process, but do not focus on the judgment process. Therefore, the judgment is only a simple if operation.
The submitted object creates an account class.
Its content is as follows:
1: /// <Summary>
2:/// Entity class of the user account
3:/// </Summary>
4:Public ClassAccount {
5:/// <Summary>
6:/// User Name
7:/// </Summary>
8:Public StringUsername {Get; set ;}
9:/// <Summary>
10:/// Password
11:/// </Summary>
12:Public StringPassword {Get; set ;}
13:}
Most examples use the account class, but not all examples use the account.
In this article, we do not focus on the controller of the page for information submission, but on the Controller for information processing.
The Controller has such a method to assist in judgment:
1: /// <Summary>
2: /// An auxiliary Judgment Method
3: /// </Summary>
4: /// <Param name = "username"> User Name </param>
5: /// <Param name = "password"> password </param>
6: /// <Returns> </returns>
7: StringAreequals (StringUsername,StringPassword)
8:{
9:Return(Username. tolower () ="Admin"& Password ="123456"). Tostring ();
10:}
Refer to the data submitted by post for transmission.
The reason why post is described here is because the post situation is more comprehensive than the get method, and the get situation is similar.
View:
1:<%Using(Html. beginrouteform (New{Controller ="Home", Action ="Process"}) {%>
2:<P>
3:<Label> User name: </label> <% = html. Textbox ("Username") %> </P>
4:<P>
5:<Label> password: </label> <% = html. Textbox ("Password") %> </P>
6:<P>
7:<Input type ="Submit"/> </P>
8:<%
9:} %>
1. Use request to obtain the submitted information from the most basic start.
1: /// <Summary>
2: /// Action for processing the request
3: /// </Summary>
4: /// <Returns> </returns>
5: PublicActionresult process ()
6:{
7:ReturnContent (
8:Areequals (request. Form ["Username"], Request. Form ["Password"])
9:);
10:}
This is not explained in detail. form is one of the most basic methods for obtaining Form submission. If you do not know about this method, your asp. net basics to be submitted, you can view ASP. netArticle.
2. Submit using the action Parameter
In comparison, this may be more beautiful.
1: /// <Summary>
2: /// Action for processing the request
3: /// </Summary>
4: /// <Param name = "username"> the two parameters must be the same as the names in the form. </param>
5: /// <Param name = "password"> </param>
6: /// <Returns> </returns>
7: PublicActionresult process (StringUsername,StringPassword)
8:{
9:ReturnContent (
10:Areequals (username, password)
11:);
12:}
3. updatemodel to get the transfer
1: /// <Summary>
2: /// Action for processing the request
3: /// </Summary>
4: /// <Returns> </returns>
5: PublicActionresult process ()
6:{
7:VaR A =NewAccount ();
8:Updatemodel ();// Confirm that A contains the password and username attributes; otherwise, an exception is thrown.
9:ReturnContent (
10:Areequals (A. username, A. Password)
11:);
12:}
Of course, exceptions may easily occur here, so you can use another method to replace it. tryupdatemodel:
1: /// <Summary>
2: /// Action for processing the request
3: /// </Summary>
4: /// <Returns> </returns>
5: PublicActionresult process ()
6:{
7:VaR A =NewAccount ();
8:// In this way, you can use another processing method when the conversion fails.
9:ReturnContent (
10:Tryupdatemodel ())? Areequals (A. username, A. Password ):Bool. Falsestring
11:);
12:}
The above updatemodel method is simple, but ASP. net mvc provides us with a simpler method:
4. Bind
1: /// <Summary>
2: /// Action for processing the request
3: /// </Summary>
4: /// <Returns> </returns>
5: PublicActionresult process (account)
6:{
7:ReturnContent (
8:Areequals (A. username, A. Password)
9:);
10:}
That's simple.
Possible problems
When submitting a form, we may have multiple account objects. What should we do in this case.
See the following:
View:
1:<%Using(Html. beginrouteform (New{Controller ="Home", Action ="Process"}) {%>
2:<P>
3:<Label> username 1: </label> <% = html. Textbox ("A. username") %> </P>
4:<P>
5:<Label> password 1: </label> <% = html. Textbox ("A. Password") %> </P>
6:<P>
7:<Label> username 2: </label> <% = html. Textbox ("B. username") %> </P>
8:<P>
9:<Label> password 2: </label> <% = html. Textbox ("B. Password") %> </P>
10:<P>
11:<Input type ="Submit"/> </P>
12:<%
13:} %>
Note the name,
The action to be processed is as follows:Code:
1: /// <Summary>
2:/// Action for processing the request
3:/// </Summary>
4:/// <Returns> </returns>
5:PublicActionresult process (account A, Account B)
6:{
7:// Process the code
8:ReturnView ();
9:}
This is all OK