Reputation: 789
This is probably a silly question.. but what the heck, I'm a bit stumped and frustrated.
I'm trying to add a REST service to my application. I want to support two types of GET which have optional parameters.
In the global ascx I have;
routes.Add(new ServiceRoute("api/userstats", new HttpServiceHostFactory() { Configuration = config }, typeof(RESTService.APIs.UserStatsAPI)));
And then in that controller I have;
[WebGet(UriTemplate = "{userguid}")]
public IQueryable<UserStat> Get(string userguid)
{
return null;
}
[WebGet(UriTemplate = "{userguid}/{genericstatid}")]
public IQueryable<UserStat> Get(string userguid, int genericstatid)
{
return null;
}
So basically you can query all the stats for a user or just the ones of a specific type. When I try to run the app, it complains because I have two definitions for GET.
Cannot have two operations in the same contract with the same name,
I can understand why, but I'm not sure how to get around it. If it was MVC I'd define different types of routing, but I'm not sure how to do that in a service route.
Thanks for any help. I'm going to go get a coffee and see if that helps.
Upvotes: 0
Views: 348
Reputation: 789
I solved the issue here, which effectively was my own lack of understanding, I assumed the method name was implicitly the type of action, i.e. a webget had to be called GET. This is not the case, so long as you have the WebGet you can call the method name something else and vary the parameters, as with Alexander's example above. And mine below;
[WebGet(UriTemplate = "{userid}")]
public IQueryable<UserStat> Get(int userid)
{
[WebGet(UriTemplate = "{userid}/{genericstatid}")]
public IQueryable<UserStat> GetWithID(int userid, int? genericstatid)
{
Upvotes: 0
Reputation: 3214
Have you tried to leave only last method with some modifications?
[WebGet(UriTemplate = "{userguid}/{genericstatid}")]
public IQueryable<UserStat> Get(string userguid, int? genericstatid)
{
if (genericstatid.HasValue)
{
// looking at one
}
else
{
// looking at all.
}
return null;
}
UPDATE:
Here is an example from my code, where there is a same named operation, but with different parameters:
/// <summary>
/// Gets the javascript representation of important enumerations
/// </summary>
/// <returns></returns>
[OperationContract, WebGet(UriTemplate = "get/javascriptEnums.js")]
Stream GetEnumsJavascriptRegistration();
/// <summary>
/// Gets some file from server
/// </summary>
/// <param name="fileName">The name of the file to return</param>
/// <returns></returns>
[OperationContract, WebGet(UriTemplate = "get/{*fileName}")]
Stream Get(string fileName);
Upvotes: 1