Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 16, 2012 12:20 PM by LittleClive
Feb 22, 2012 07:55 AM|LINK
I’m looking for a way to implement versioning. In my opinion a custom ActionMethodSelector is a good starting point. But the interface IActionMethodSelector is internal. Do I have to implement a custom IHttpActionSelector or is there
an easier way?
Feb 22, 2012 09:22 AM|LINK
Actually, ActionNameSelectorAttribute is called before ActionMethodSelectorAttribute. Simone Chiaretta covers the topic in his excellent article on
ASP.NET MVC Extensibility.
From the article (step 4 if you visit the link):
The first part of the process involves identifying the method to execute: in the simple scenario, the method which shares the same name as the action is chosen, but this behavior can be modified by applying an attribute of
typeActionNameSelectorAttribute, which allows you to apply custom logic to this process. If more than one method is found, the correct
one is chosen with the help of another kind of attribute applied to the methods:ActionMethodSelectorAttribute.
To create an ActionMethodSelectorAttribute, the preferred way is to inherit from the base class ActionMethodSelectorAttribute and override the parts you need to.
Feb 22, 2012 10:34 AM|LINK
The ActionMethodSelectorAttribute is an mvc thing. in a web api scenario the attribute will never be called. The web api way is to implement IActionMethodSelector or IActionHttpMethodSelector. But both interfaces are internal.
Feb 23, 2012 03:51 AM|LINK
I’m looking for a way to implement versioning. In my opinion a custom ActionMethodSelector is a good starting point.
Could you expand on the scenario you're trying to enable. I'm not sure I follow how implementing versioning (could you clarify what you mean by that) necessarily requires or implies a custom ActionMethodSelector.
Feb 23, 2012 06:54 AM|LINK
I'm looking for a way/pattern to implement breaking changes in my api. So i have to separate the calls and assign it to the old or new api. In my scenario a versionfilterattribute will provide the necessary information to choose the
public Entity1 Put(Entity1 entity)
//Implementation for the "old" version
public Entity2 Put(Entity2 entity)
//Implementation for the "new" version
Or is it better to implement a custom message handler and rewrite the url and map the routes to different controller?
Mar 14, 2012 08:46 AM|LINK
*bump* Did you get anywhere with this?
Mar 16, 2012 12:20 PM|LINK
Would plugging in your own System.Web.Http.Controllers.IHttpActionSelector help? You can do this via a custom IDependencyResolver and the interface is public.
Having said that, from looking at the ApiControllerActionSelector which is the default implementation it looks like you'd have to do a lot of work. It's a shame some of those methods aren't virtual.
As a potentially hacky approach could you adapt/wrap the ApiControllerActionSelector in your own IHttpActionSelector that modifies the action in the controllerContext based on the version (e.g. adds V1 or V2) on the end and then change your method name to
PutV1 and PutV2 - I have no idea if this would work and it smells a bit.
You may have already discounted this but we opted for using media type formatters to transform for different versions instead of switching the action. Happy to explain more if interested.