Go Introduction to ASP. 4, Controller and action

Source: Internet
Author: User

Controller is a more important part of MVC. Almost all of the business logic is handled here, and the data is taken out of the model. In ASP. PREVIEW5, the original controller class is divided into the controller class and the Controllerbase class. The controller class inherits from the Controllerbase class, and the Controllerbase implementation is the IController interface.

Controllerbase implements the Execute method of the IController interface, which, after the route is matched to the controller, invokes the Execute method to enter the controller's processing. It also defines an abstract method Executecore method, which is called at the end of the Execute method. Controllerbase also defines three core attributes. We will discuss tempdata and ViewData in detail later.

In addition to inheriting from the Controllerbase class, the Controller class implements several filter interfaces, which we discuss later in detail.

Public abstract class Controller:controllerbase, Iactionfilter, Iauthorizationfilter, IDisposable, Iexceptionfilter, iresultfilter{}


The controller class also defines a number of useful methods, and our new controller must inherit from this controller class. For example, we create a new admincontroller:

public class Admincontroller:controller
{
}

Action method

Let's talk about the more important action methods in the controller. In ASP., the URL is mapped to an action in the controller, and then the matching action handles our business logic and returns to the view.

The public method in the controller is treated as an action method. The action method typically returns a actionresult result. 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 route that matches the URL of the action method.) For example, Url:home/index, where Index is the action name). Why do you mention this action name here? The action name should be defined and defined using Actionnameattribute. Take a look at the following example:

Public ActionResult Setting ()
{
throw new NotImplementedException ();
}

[ActionName ("Setting")]
Public ActionResult SaveSetting ()
{
throw new NotImplementedException ();
}

The action name of these two action methods is "Setting", that is, for url:admin/setting, it can be matched to these two action methods at one time. If a URL matches two action methods at the same time, the program throws an error:

What do we do if we want the action name of the two action to be setting,setting () to display a form page to the user, and SaveSetting () to save the form data submitted by the user? We can use Acceptverbsattribute to set the attribute that defines the action method to match 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 ();
}

Thus, for a client request that HttpMethod is "GET", it will match to setting () to display a form to the user, and if the user posts back the form data, it will match to SaveSetting (), We can process the user post data and save it to the database.

Here Acceptverbsattribute is inherited from the Actionselectionattribute, we can also inherit from Actionselectionattribute to customize the function we want to implement. We will explain this in detail later. If you're impatient, you can look at the ASP. NET MVC Preview 5 Experience-implement Actionselectionattribute to determine whether or not to choose a different action for AJAX requests.

If you want to set a public method to not the action method, you will add nonaction attribute for the public method:

Arguments to the action method

For example, we want to define an action method for editing the log in Admincontroller:

Public ActionResult editpost (int? id)
{
throw new NotImplementedException ();
}

For URL:ADMIN/EDITPOST/2, the above parameters are automatically assigned a value of 2. When the route is matched, ASP. NET MVC automatically assigns a value to the action method's parameters according to the route's settings. Therefore, the previous ID parameter will be automatically assigned to 2, as long as the route configuration, the ID parameter must be specified, for example:

Routes. MapRoute (
"Default",//Name of Route
' {controller}/{action}/{id} ',//url with parameter
New {controller = "Home", action = "Index", id = ""}//Set default parameters
);

If we modify the route to:

Routes. MapRoute (
"Default",//Name of Route
' {Controller}/{action}/{para} ',//url with parameter
New {controller = "Home", action = "Index", para = ""}//Set default parameters
);

Then the parameters of the previous action method must be modified to public actionresult editpost (int?). para) {}, the parameters of the action method are the same as the parameter names defined in the route, and ASP. NET MVC will automatically assign values to the parameters of the action method.

ActionResult

The action method returns the result of the ActionResult type. ASP. NET MVC provides several ActionResult implementations for us, as follows:

In general, our controller may have some of the same situation, for example, we may be in each controller in error or when we want to display a message to the user, or some common data to be presented. At this point, we'd better define a base class for our own controller:

public class Basecontroller:controller
{
Public Basecontroller ()
{

}

protected ActionResult showmsg (list<string> msgs)
{
throw new NotImplementedException ();
}

Public ActionResult Message ()
{
throw new NotImplementedException ();
}
}

The other controllers are then inherited from this basecontroller:

public class Admincontroller:basecontroller
{
[Acceptverbs ("GET")]
Public ActionResult Setting ()
{
throw new NotImplementedException ();
}

[ActionName ("Setting"), Acceptverbs ("POST")]
Public ActionResult SaveSetting ()
{
throw new NotImplementedException ();
}

Public ActionResult editpost (int? id)
{
throw new NotImplementedException ();
}
}

Well, it's not too early, let's get here first. enjoy! Post by Q.lee.lulu.

Go Introduction to ASP. 4, Controller and action

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.