skip to the main content area of this page
Patterns and Practices

 

ASP.NET MVC NonActionAttribute for Decorating Controller Methods - ActionMethodSelectorAttribute


The ActionMethodSelectorAttribute is pretty ingenious in the ASP.NET MVC Framework.

 

 

ActionNameAttribute - Change Action Name of Controller Method

There is the ActionNameAttribute that allows one to rename the Action Name of a method on a controller. By default, the action name of a controller method is the method name. The ActionNameAttribute that allows one to rename the method by supplying the substitute name in the ActionNameAttribute:

 

// Example of Using ActionName

[ActionName("Delete"), AcceptVerbs(HttpVerbs.Post)]

public ActionResult Remove(int id)

 

In the case above, the Controller Remove Method no longer responds to the Remove Action, but instead responds to the Delete Action.

 

 

NonActionAttribute on ASP.NET MVC Controller Methods

The NonActionAttribute is another example of the ActionMethodSelectorAttribute. If you do not want a method on a Controller in the MVC Framework to respond to an action command, you can decorate it with a NonActionAttribute.

 

// Example of Using NonActionAttribute

[NonAction]

public ActionResult Remove(int id)

 

In the case above, the Remove Method on the Controller will not respond to the Remove Action Request.

 

 

Conclusion

These are a couple of examples of the ActionMethodSelectorAttribute. You can create custom attributes if you wish as well.

 

 

ASP.NET MVC Tutorials


Tags: MVC


Topics



 

Popular Tags



Recent Links