I. Basic concepts:
The controller involves three parts: controller, action, and actionresult ).
Traditional web frameworks, such as ASP/PHP/ASP. NET web forms, all request URLs are mapped to specific files. For example, the request URL, such as/products. aspx or/products. php, is processed by the products. aspx or products. php file. The web-based MVC framework maps URL addresses to the server-side code a little different. Instead of ing URL addresses to specific files, it maps to the class methods. These classes are controller controllers in MVC. They are responsible for processing incoming requests and user input, receiving and storing data, and return the result to the client (displaying HTML, downloading files, or redirecting to different URLs ).
2. Controller ):
Right-click the controllers folder in the web project and choose add> controller, as shown in.
In the Add controller dialog box that appears, enter guestbookcontroller as the controller name and click Add. OK. We have added the guestbookcontroller. CS file to the project file.
The Code is as follows:
Public class guestbookcontroller: Controller
{
//
// Get:/guestbook/
Public actionresult index ()
{
Return view ();
}
}
Note: The controller must end with the Controller in the format of xyzcontroller. CS.
3. Action method:
1. The action that can be accessed through a URL must be a public method;
2. If the [nonaction] attribute is marked, it cannot be accessed through a URL;
For example:
[Nonaction]
Public actionresult index ()
{
Return view ();
}
It cannot be accessed through guestbook/index.
3. By default, the action method name is the action name, and the action name is the name that can be accessed through the URL.
If necessary, you can specify the action name. For example:
[Actionname ("default")]
Public actionresult index ()
{
Return view ("Index"); // if the action name is changed, you must specify the view name. If this parameter is not specified, the default view file is displayed.
}
In this way, you can access this action through guestbook/default.
4. Two common attributes are [httppost] and [httpget]. For example:
[Httppost]
Public actionresult index ()
{
Return view ();
}
Indicates that this method is responsible for handling http post actions.
You can also set it through [acceptverbs (httpverbs. Post)]. [httppost] works the same, but it is easy to develop quickly.
After the [httppost] attribute is added to the overloaded action method, ASP. net mvc automatically distributes requests based on the incoming HTTP action.
Give the appropriate action method. For example:
[Httppost]
[Actionname ("Index")]
Public actionresult indexpost ()
{
Return view ();
}
Public actionresult index ()
{
Return view ("Index ");
}
When a URL requests guestbook/index, if it is an http post request, it is executed by the method named indexpost. If it is another request, it is executed by the method named index.
For more parameters other than post and get, see ):
Http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
Or search for acceptverbs.
5. Filter attribute action filter:
Another type of attribute that can be placed on action is filter ).
You can use filters to add cache, verification, and error handling.
For example, the [handleerror] attribute.
Remove the [handleerror] attribute and add the following code:
Public actionresult handleerror ()
{
Throw new exception ("exception ");
}
Access home/handleerror. If an error occurs, some source code is exposed on the exception page, which poses a hidden danger to the website.
Now, set the [handleerror] attribute.
[Handleerror]
Public class homecontroller: Controller
Then, find the <customerrors> section in Web. config, set mode to "on", and remove comments. For example.
Run home/handleerror again to display the error. aspx personalized exception page in the shared directory.
This is an example of a filter. For more information, see the filter section below.
Note: This attribute can be applied to a single action method or controller class, so that it can be applied to all actions of the controller class.
6. Parameters of the Action method:
For example, the URL is guestbook/index/12345,
The code for the index action is:
Public actionresult index (int id)
{
Int myid = ID;
Return view ("Index ");
}
After debugging this URL, we will find that the value of myid is 12345.
We do not use code similar to int id = convert. toint32 (request ["ID.
This is because:
By default, the parameter values of the Action method are retrieved from the requested dataset.
Datasets include form, URL routing data, querystring, and cookie values.
The data of the Action parameter will first be taken from the form in the current request, followed by the route values parsed by route, and finally get the value that has not been repeated from querystring. Repeated occurrences take the first value as the priority.
4. Controller result ):
1. Successfully executing the MVC controller operation will generate an object derived from actionresult.
2. The types of actionresult derivation include:
Viewresult. Present the view page to the client. Returned by the view method.
Emptyresult. No HTTP response is written.
Redirectresult. Redirect to another URL. Returned by the Redirect method.
Redirecttorouteresult. Redirect to another route. Returned by the redirecttoaction and redirecttoroute methods.
Filecontentresult indicates a downloadable file, which is returned using the file method.
Contentresult. Return the action result as plain text. Returned by the content method.
Jsonresult. The JSON result is returned. Returned by the JSON method.
Javascriptresult. A JavaScript Object is returned by the javascriptresult method.
3. You can also create action methods that return any type, such as strings, integers, or boolean values. The returned values are encapsulated into the appropriate actionresult type before they are presented to the returned stream. For example, if the return type is set to string or date, it is automatically converted to contentresult type and returned.
In the next section, we will introduce the view.