I'm running into what I think is a weird problem in the RC version of the Web API (looking forward to the upcoming RTM). My understanding is that one can overload methods as long as they have different signatures. I'm running into an issue with this if
the parameter type is an enum but it is fine if it is a string.
public IEnumerable<Client> Get()
{
List<Client> clients = new List<Client>()
{
};
// Implementation left out for simplicity
return clients;
}
public IEnumerable<Client> Get(ClientStatus status)
{
List<Client> clients = new List<Client>();
// Implementation left out for simplicity
return clients;
}
Gives me the following error:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
Multiple actions were found that match the request: System.Collections.Generic.IEnumerable`1[SnapDragon.Common.Management.Client] Get() on type SnapDragon.Server.WebApp.Main.Controllers.Api.ClientsController System.Collections.Generic.IEnumerable`1[SnapDragon.Common.Management.Client] Get(SnapDragon.Common.Management.ClientStatus) on type SnapDragon.Server.WebApp.Main.Controllers.Api.ClientsController
</string>
However if I switch the status to take in a string and then manually parse it back to its enum value within the method it works fine. I thought that the WebAPI framework though could deal with enum values. Any ideas what is going on?
I am just using the default routes. ClientStatus is a basic enum type.
I was hopeful that maybe the issue wouldn't exist in the RTM but I see it does. I spent some time trying to figure out where the issue was in the MVC source code. I got close I think to finding the problem the other night but had to call it a night before
I found the actual location of the error. I was hoping once I understood where it was to find a work around. I tried just explicitly setting a model binder attribute on the parameter but I noticed my custom model binding never got called. After tracing
the code I realized it is because the enum parameter type isn't recognized when the framework builds the cache of method overloads so when it tries to find the correct action for a request it sees both my methods as having no parameters. Is there some sort
of a work around you know of to make this work?
My string approach stops working when I need to build another action method that takes in a single string. The compiler won't let me have two get methods that take in a single string parameter (for obvious reasons). Thus I'm stuck if I can't get the enum
overload to work.
greensombrer...
Member
2 Points
12 Posts
Use of enum parameter and method overloading
Aug 10, 2012 04:52 AM|LINK
I'm running into what I think is a weird problem in the RC version of the Web API (looking forward to the upcoming RTM). My understanding is that one can overload methods as long as they have different signatures. I'm running into an issue with this if the parameter type is an enum but it is fine if it is a string.
public IEnumerable<Client> Get() { List<Client> clients = new List<Client>() { }; // Implementation left out for simplicity return clients; } public IEnumerable<Client> Get(ClientStatus status) { List<Client> clients = new List<Client>(); // Implementation left out for simplicity return clients; }Gives me the following error:
However if I switch the status to take in a string and then manually parse it back to its enum value within the method it works fine. I thought that the WebAPI framework though could deal with enum values. Any ideas what is going on?
I am just using the default routes. ClientStatus is a basic enum type.
Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: Use of enum parameter and method overloading
Aug 11, 2012 02:34 PM|LINK
Yeah, this is a bug. I opened the following issue for tracking:
http://aspnetwebstack.codeplex.com/workitem/312
Thanks for reporting this!.
Kiran Challa
greensombrer...
Member
2 Points
12 Posts
Re: Use of enum parameter and method overloading
Aug 17, 2012 03:21 PM|LINK
I was hopeful that maybe the issue wouldn't exist in the RTM but I see it does. I spent some time trying to figure out where the issue was in the MVC source code. I got close I think to finding the problem the other night but had to call it a night before I found the actual location of the error. I was hoping once I understood where it was to find a work around. I tried just explicitly setting a model binder attribute on the parameter but I noticed my custom model binding never got called. After tracing the code I realized it is because the enum parameter type isn't recognized when the framework builds the cache of method overloads so when it tries to find the correct action for a request it sees both my methods as having no parameters. Is there some sort of a work around you know of to make this work?
My string approach stops working when I need to build another action method that takes in a single string. The compiler won't let me have two get methods that take in a single string parameter (for obvious reasons). Thus I'm stuck if I can't get the enum overload to work.
Thanks,
Bryan