This past Monday was the ASP.NET MVC Firestarter Event in Tampa Florida. This was a great event with over 140 people registered and the Tampa MPR filled to capacity.
I presented a couple of sessions. One session walked-thru a sample MVC LINQ To SQL Application that performed your basic CRUD operations to SQL Server 2008. Although conceptually simple, the application showed numerous features in ASP.NET MVC.
ActionNameAttribute in the MVC Framework
Although not rocket science, I showed use of the ActionNameAttribute that allows one to override the name of the action which defaults to the name of the method. In the case below, the action is Delete and not Remove due to the ActionNameAttribute.
// Example of Using ActionName
[ActionName("Delete"), AcceptVerbs(HttpVerbs.Post)]
public ActionResult Remove(int id)
One of the questions that popped up is if the MVC Framework would also respond to a Remove Post Request. I knew the answer was no as I have tried it, but it bothered me that I couldn’t expand anymore on the subject. Here is the answer
ActionMethodSelector Class
After check the MVC Source Code, it turns out there is a ActionMethodSelector Class that looks for appropriate methods to respond to an action request. It first looks for Aliased and Non-Aliased Methods on the controller. Aliased Methods are those methods that have the ActionNameAttribute ( actually the ActionNameSelectorAttribute from which the ActionNameAttribute derives ). It separates these from Non-Aliased Methods.
AliasedMethods = Array.FindAll(actionMethods,
IsMethodDecoratedWithAliasingAttribute);
NonAliasedMethods = actionMethods.Except(AliasedMethods).
ToLookup(method => method.Name, StringComparer.OrdinalIgnoreCase);
When the ActionMethodSelector Class looks for a proper action, it uses the name supplied in the ActionNameAttribute for Aliased Actions and totally disregards the name of the method, so the method name never comes into play. Therefore, Remove, is never known as a valid action name. I left out the code as it isn’t that interesting, but the method that looks for matches in aliased methods is as follows:
internal List<MethodInfo> GetMatchingAliasedMethods
(ControllerContext controllerContext, string actionName) {
// find all aliased methods which are opting in to this request
// to opt in, all attributes defined on the method must return true
…
}
Conclusion
Not necessarily the most interesting information, but I wanted to follow-up on the question a bit more to at least satisfy my own curiosity.
Hope this helps,
Recent Posts: