ActionSelectionAttribute is an abstract base class provided by ASP. net mvc Preview 5. By naming ActionSelectionAttribute, we can guess that this Attribute is used to select (MATCH) Action methods. This abstract class only provides an abstract method IsValidForRequest, which is called in the ActionInvoker of the Controller. If this Attribute is added to an Action, the current request matches the Action only when the IsValidForRequest method returns true.
Acceptverbsattritionis an implementation of ActionSelectionAttribute. AcceptVerbsAttribute is used to select different operations for the same Action for different httpmethods (such as "GET", "POST", "DELETE", and so on.
For example, the following is an application of AcceptVerbsAttribute:
Note: The two Edit Method signatures cannot be the same. --
The working principle is probably that all actions with the AcceptVerbsAttribute feature added will call IsValidForRequest () to check whether the HTTPMethod in the current HTTP request is GET or POST. If it is consistent with the configuration, the Action will match. If more than one Action is matched (for example, if both of the above are set to "GET"), an exception is thrown. If none of them are matched, the system callsHandleUnknownAction() Method.
Okay. Now let's start implementing the questions we mentioned in the title. Simply put, it inherits the ActionSelectionAttribute class and implements its IsValidForRequest () method. I want to expose an IsAjax attribute to set whether the Action is used for Ajax requests or general requests:
GetByAjaxAttribute
Public class GetByAjaxAttribute: ActionSelectionAttribute
{
Public GetByAjaxAttribute (bool isAjax)
{
This. IsAjax = isAjax;
}
/** // <Summary>
/// First, we need an attribute to set whether the Action is used for Ajax requests or general requests.
/// </Summary>
Public bool IsAjax
{
Get;
Set;
}
Public override bool IsValidForRequest (ControllerContext controllerContext, System. Reflection. MethodInfo methodInfo)
{
// If it is an Ajax request and the IsAjax value is set to true, true is returned.
// Returns true if it is not an Ajax request and IsAjax is set to false.
// Others return false
}
}
Hey, do you think it is very simple? Then we will finish implementing the IsValidForRequest () method. The complete code is as follows, all of which are commented out:
GetByAjaxAttribute
Public class GetByAjaxAttribute: ActionSelectionAttribute
{
Public GetByAjaxAttribute (bool isAjax)
{
This. IsAjax = isAjax;
}
/** // <Summary>
/// First, we need an attribute to set whether the Action is used for Ajax requests or general requests.
/// </Summary>
Public bool IsAjax
{
Get;
Set;
}
Public override bool IsValidForRequest (ControllerContext controllerContext, System. Reflection. MethodInfo methodInfo)
{
/** // If you use a JS framework such as jQuery to perform Ajax request operations, you can use the following statement to determine
// String xhr = controllerContext. Controller. ControllerContext. HttpContext. Request. Headers ["X-Requested-With"]? "";
// MicrosoftAjax is used for asynchronous requests. It will add a "_ MVCASYNCPOST" identifier to form. Can I use this identifier for judgment?
String xhr = controllerContext. Controller. ControllerContext. HttpContext. Request. Form ["_ MVCASYNCPOST"]? "";
Return! (IsAjax ^ (xhr. ToLower () = "true "));
}
}
(For details about how to identify Ajax requests, see my other article:Idea of marking AJAX asynchronous requests)
An example of using this Attribute is as follows:
Code
[ActionName ("Add"), AcceptVerbs ("GET")]
Public ActionResult AddByGet ()
{
ViewData ["Message"] = "access through GET ";
Return View ();
}
[ActionName ("Add"), AcceptVerbs ("POST"), GetByAjax (true)]
Public ActionResult AddByAjax ()
{
// Do Your Thing Here
Return Content ("access via ajax post ");
}
// Add GetByAjax (false) here,
// Otherwise, this is also qualified when POST is sent,
// It will match two actions (this is the same as the above one), and an exception will be thrown.
[ActionName ("Add"), AcceptVerbs ("POST"), GetByAjax (false)]
Public ActionResult AddByPost ()
{
// Do Your Thing here
ViewData ["Message"] = "access through POST ";
Return View ();
}
Finally, the sample code of this article: ActionSelectionAttributeDemo.rar
Enjoy!
Reference: How a Method Becomes An Action