4. Controller and action

Source: Internet
Author: User

This series of articles is based on ASP. net mvc preview5.

Controller is an important part of MVC. Almost all business logic is processed here and data is retrieved from the model. In ASP. net mvc preview5, the original controller class is divided into the Controller class and the controllerbase class. The Controller class inherits from the controllerbase class, while the controllerbase implementation is the icontroller interface.

Controllerbase implements the execute method of the icontroller interface. After the route matches the controller, the execute method is called to process the controller. An abstract method executecore is also defined here. This method will be called at the end of the execute method. Controllerbase also defines three core attributes. We will discuss tempdata and viewdata in detail later.

The Controller class not only inherits from the controllerbase class, but also implements several filter interfaces. We will discuss filter in detail later.

Public abstract class Controller: controllerbase, iactionfilter, iauthorizationfilter, idisposable, iexceptionfilter, iresultfilter {}

The Controller class also defines many useful methods. The newly created controller must inherit from this controller class. For example, create an admincontroller:

Public class admincontroller: Controller
{
}

 

Action Method

Next, let's talk about the important action methods in the controller. In ASP. net mvc, URLs are mapped to an action in the controller, and then matched actions are used to process our business logic and return the view.

All public methods in controller are treated as action methods. The action method usually returns the result of an actionresult. For example, we define a setting action method for the previous admincontroller to set some basic parameters of the blog:

Public class admincontroller: Controller
{
Public actionresult setting ()
{
Throw new notimplementedexception ();
}
}

 

By default, the method name of the Action method is the action name of the action (the Action name refers to the part of the URL in the route that matches the action method. For example, URL: Home/index, where index is the action name ). Why do we mention this action name here? The action name must be customizable and can be defined using actionnameattribute. See the following example:

Public actionresult setting ()
{
Throw new notimplementedexception ();
}

[Actionname ("setting")]
Public actionresult savesetting ()
{
Throw new notimplementedexception ();
}

 

The action names of the two action methods are "setting". For URL: admin/setting, the two action methods can be matched at the same time. If a URL matches two action methods at the same time, the program will throw an error:

If we want the action names of both actions to be setting, setting () is used to display a form page to the user, and savesetting () is used to save the form data submitted by the user, what should we do? We can use acceptverbsattribute to set this attribute. This attribute defines that the action method matches the specified httpmethod. For example, the following code:

[Acceptverbs ("get")]
Public actionresult setting ()
{
Throw new notimplementedexception ();
}

[Actionname ("setting"), acceptverbs ("Post")]
Public actionresult savesetting ()
{
Throw new notimplementedexception ();
}

 

In this way, for client requests whose httpmethod is "get", it will match setting () to display a form to the user. If the form data returned by the user post is, it will match the savesetting (), so that we can process the user's post data and save it to the database.

Here, acceptverbsattritioninherits from actionselectionattribute, and we can also inherit from actionselectionattribute to customize the features we want to implement. This will be explained in detail later. If you are impatient, you can take a look at Asp.net MVC preview 5 experience-implement actionselectionattribute to determine whether to select different actions for Ajax requests.

If you want to set a public method as not an action method, you need to add the attribute of nonaction for the public method:

Parameters of the Action Method

For example, we need to define an action method for log editing in admincontroller:

Public actionresult editpost (Int? ID)
{
Throw new notimplementedexception ();
}

 

For URL: admin/editpost/2, the above parameter is automatically assigned to 2. ASP. net mvc will automatically assign values to the parameters of the Action Method Based on the route settings when matching the route. Therefore, the prerequisite that the previous ID parameter is automatically assigned to 2 is that the ID parameter must be specified during route configuration. For example:

Routes. maproute (
"Default", // route name
"{Controller}/{action}/{ID}", // URL with Parameters
New {controller = "home", Action = "Index", id = ""} // set the default parameter
);

 

If we change route:

Routes. maproute (
"Default", // route name
"{Controller}/{action}/{para}", // URL with Parameters
New {controller = "home", Action = "Index", para = ""} // set the default parameter
);

 

The parameter of the preceding action method must be changed to public actionresult editpost (Int? Para) {}, so that the parameters of the Action method are the same as those defined in the route, ASP. net mvc can automatically assign values to parameters of the Action method.

Actionresult

The action method returns the result of the actionresult type. ASP. net mvc provides the implementation of several actionresult types, as follows:

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.