ASP. net mvc learning notes ----- ActionInvoker

Source: Internet
Author: User

Still in this figure: When the ControllerFactory generates the Controller instance, you need to use ActionInvoker to choose to call an appropriate Action for execution. The base class Controller provided by ASP. net mvc has implemented the ActionInvoker function. However, we can implement custom ActionInvoker to replace the ActionInvoker provided in the framework. First, an ActionInvoker needs to implement the IActionInvoker interface: public interface IActionInvoker {bool InvokeAction (ControllerContext controllerContext, string actionName);} There are two InvokeAction parameters, of which controllerContext contains the current Controller information and request information, the actionName is the name of the called action. Returns true if the function finds an appropriate Action and calls it successfully. Otherwise, returns false. Then how can we use the custom ActionInvoker? The Controller class provides the ActionInvoker attribute, so we can set this attribute so that MVC can use our ActionInvoker: public class HomeController: controller {public HomeController () {this. actionInvoker = new MyActionInvoker ();}//.... other code .....} the built-in ActionInvoker in MVC also mentioned above. The base class Controller implements the ActionInvoker function, which uses the built-in ActionInvoker ----- ControllerActionInvoker of MVC. To use ControllerActionInvoker as an Action, a method must meet the following conditions: 1. the scope of this method must be public 2. this method cannot be a static method 3. this method cannot be a Controller base class method 4. this method cannot be a constructor. In addition, if the method is a generic method that meets the preceding conditions, an exception is thrown by default when you try to call the method, controllerActionInvoker takes the method with the same name as actionName as Action and calls it. But what if a Controller has multiple overload methods with the same name? We can use the ActionNameAttribute feature to set the Action alias: public class HomeController: Controller {[ActionName ("Enumerate")] public ActionResult List () {return View () ;}} so that when we request/home/enumerate, The called Action is List, but when we request/home/list, list will not be called. In this example, the 404 page will be returned. In addition to the alias, ControllerActionInvoker also uses the action method selection (select preference) mechanism to handle how to select multiple methods with the same name. Let's look at the following code: [HttpGet] public ViewResult MyAction (int? Id) {return View (id);} [HttpPost] public ViewResult MyAction (Product product) {return View (product);} it should be clear to everyone, in a get request, MyAction (int? In the post request, MyAction (Product product) is called ). This is action method selection. We can create a custom Action Method selector to set the choice preference of ControllerActionInvoker. Public class ActionMethodSelectorAttribute: Attribute {public bool IsValidForRequest (ControllerContext controllerContext, MethodInfo methodInfo) {return controllerContext. httpContext. request. isLocal ;}} the selector must have the IsValidForRequest method. If the return value of the method is true, it indicates that the method meets the preference and will be called preferentially. ControllerActionInvoker does not find an appropriate Action: in this case, the Controller's HandleUnknowAction method will be called. This method will display the 404 page, but we can override this method to change the default behavior: protected override void HandleUnknownAction (string actionName) {Response. write (string. format ("Action not found: {0}", actionName ));}

Related Article

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.