Optional Parameters on GET not functioning as expected.

Topics: Web Api
Oct 6, 2011 at 8:49 AM

Hi there,

 

I think this is an issue (or something changed) with the new version of the webapi. If i don't pass the exact amount of parameters then it always enters in the method that is decorated with a GET only. But the parameters that you pass on the URL of a GET are optional. Before i believe if i don't pass parameter then this parameter was NULL etc. But now if i decide not to pass 1 parameter then it doesn't enter in the correct method.

 

I would really appreciate any help.

 

I have 2 signatures

        public List<Items> GetSessions(HttpRequestMessage request)
        {
           
        }
 
        public GroupedPagingContainer<Items> GetGroupedSessions(HttpRequestMessage request, string page, string pageSize, string orderBy, string filterQuery, string filterParameters)
        {
       }

The interfaces are decorated with the GET attribute like so

[WebGet(UriTemplate = "")]
        List<Items> GetSessions(HttpRequestMessage request);
 
        [WebGet(
            UriTemplate =
                "?page={page}&pageSize={pageSize}&orderBy={orderBy}&filterQuery={filterQuery}&filterParameters={filterParameters}"
            )]
        GroupedPagingContainer<Items> GetGroupedSessions(
            HttpRequestMessage request,
            string page,
            string pageSize,
            string orderBy,
            string filterQuery,
            string filterParameters);

Imagine if i pass this

/sessions?filterQuery=y&pageSize=5&page=1&filterParameters=%20m&orderBy=TaskName&sortOrder=1

then sure enough it enters in the method GetGroupedSessions, great!

Now imagine if i pass this (notice the missing filterParameters)

/sessions?filterQuery=y&pageSize=5&page=1&orderBy=TaskName&sortOrder=1

Now it enters in GetSessions, which is wrong.

Am i missing something here, i am sure before it used to continue entering in the method (GetGroupedSessions) but the value of filterParameters would be NULL


Any ideas what i am missing here?

Thanks.
Oct 13, 2011 at 10:24 AM

Bump, appreciate anybody has any input on this.

 

Currently i am still stuck, this seems to have started when i updated.

 

Any ideas? Anyone?

Oct 13, 2011 at 5:04 PM

Which version did you upgraded from?  I tried this on Preview4 and Preview3 and I still see the same Uri template matching as you described.

Having said that, I have tried removing the GetSessions serivce operation and I do see the query with the filterParameters missing matches the GetGroupedSessions service operation (with the filterParameters being NULL).  Is there by any chance that the GetSessions service operation was added in recently?

Oct 14, 2011 at 10:35 PM

Hi there, thanks for the reply.

 

Yes exactly i believe it was working perfectly in Preview 4, i am now on preview 5... and it is no longer working.

 

I am sure its not broke but probably something that i need to enable ????

 

If you don't pass a parameter then it never enters that method with the parameter being NULL

 

You have to specifcally add EVERY single parameter

Oct 14, 2011 at 11:01 PM

> Yes exactly i believe it was working perfectly in Preview 4, i am now on preview 5... and it is no longer working.

I said I have tried it on Preview4 and Preview5, and this query (without filterParameters) always hit GetSessions(...):

/sessions?filterQuery=y&pageSize=5&page=1&orderBy=TaskName&sortOrder=1

> If you don't pass a parameter then it never enters that method with the parameter being NULL.  You have to specifcally add EVERY single parameter

That is not true.  Try temporary removing the GetSessions(...) and this query (without filterParameters) will hit your GetGroupedSessions(...):

/sessions?filterQuery=y&pageSize=5&page=1&orderBy=TaskName&sortOrder=1

I am guessing you have added the GetSessions(...) recently, is that true?  The UriTemplate for GetSessions(...) seems to take precedence over the one for GetGroupedSessions(...).  I am asking because wihtout GetSessions(...), it will hit GetGroupedSessions(...) with the filterParameters NULL.

Oct 21, 2011 at 7:54 AM

Hi Maggieying, Yep! sorry for the delay, i was testing. This is exactly what happens. Thank you for your help.

 

Now i am hitting the correct method and it passes null to the parameter that was missing. While this is great :-) I do need the other method in there.

 

What tells the webapi which takes precedence ? I thought we could have more than 1 method with different parameters and return type in the webapi.

 

I would really appreciate any feedback you have here.

 

Thanks for your response

Oct 21, 2011 at 5:04 PM

In this case, the empty UriTemplate for your GetSessions(...) will take precedence if you have missing query parameter (i.e. without filterParameters).

Here is my suggestion.  Can you modify your UriTemplate to the following:

        [WebGet(UriTemplate = "Sessions")]
        List<Items> GetSessions(HttpRequestMessage request);

        [WebGet(
            UriTemplate =
                "Groupedessions?page={page}&pageSize={pageSize}&orderBy={orderBy}&filterQuery={filterQuery}&filterParameters={filterParameters}"
            )]
        GroupedPagingContainer<Items> GetGroupedSessions(
            HttpRequestMessage request,
            string page,
            string pageSize,
            string orderBy,
            string filterQuery,
            string filterParameters);

This way, both of these Uri will hit the GetGroupedSessions operation:

/groupedsessions?filterQuery=y&pageSize=5&page=1&filterParameters=%20m&orderBy=TaskName&sortOrder=1
/groupedsessions?filterQuery=y&pageSize=5&page=1&orderBy=TaskName&sortOrder=1

This will hit the GetSessions operation:

/sessions

Will this work for you?

Thanks,
Maggie Ying