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?
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.
Darrell Norton, MVP
Darrell Norton's Blog Please click "Mark as Answer" if this helped you.
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.
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.
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
right method.
[Version("1.0")]
public Entity1 Put(Entity1 entity)
{
//Implementation for the "old" version
}
[Version("1.1")]
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?
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.
SixiS
Member
50 Points
16 Posts
Custom ActionMethodSelector
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?
DarrellNorto...
All-Star
86743 Points
9643 Posts
Moderator
MVP
Re: Custom ActionMethodSelector
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.
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
SixiS
Member
50 Points
16 Posts
Re: Custom ActionMethodSelector
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.
marcind
Contributor
3344 Points
609 Posts
Microsoft
Re: Custom ActionMethodSelector
Feb 23, 2012 03:51 AM|LINK
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.
ASP.NET Team
@marcind
Blog
SixiS
Member
50 Points
16 Posts
Re: 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 right method.
[Version("1.0")] public Entity1 Put(Entity1 entity) { //Implementation for the "old" version } [Version("1.1")] 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?
theshadow330
Member
10 Points
17 Posts
Re: Custom ActionMethodSelector
Mar 14, 2012 08:46 AM|LINK
*bump* Did you get anywhere with this?
LittleClive
Member
91 Points
65 Posts
Re: Custom ActionMethodSelector
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.