Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$http.get with null parameters are not hitting the Web API controller

I am trying to get to Web API GET controller using $http.get in angular application as follows :

$http.get(BasePath + '/api/documentapi/GetDocuments/' , 
                                {
                                    params: {
                                        PrimaryID: ID1,
                                        AlternateID: ID2,                                            
                                    }
                                }).then( ...

In my case, either the PrimaryID or the AlternateID will have the value. So, one of them will be null always.

My Web api method is

public DocumentsDto[] GetDocuments(decimal? PrimaryID, decimal? AlternateID)
    { ...

When one of the value is null, the url generated by $http.get is as follows :

http://BaseServerPath/api/documentapi/GetDocuments/?PrimaryID=1688 

or

 http://BaseServerPath/api/documentapi/GetDocuments/?AlternateID=154

This does not hit my Web API method.

However if I use

http://BaseServerPath/api/documentapi/GetDocuments/?PrimaryID=1688&AlternateID=null

it works. I can hardcode the values to null in my params, however I would like to know if there is any correct way to achieve this.

Thanks, Sam

like image 285
Sumesh Kuttan Avatar asked Dec 17 '15 13:12

Sumesh Kuttan


1 Answers

I got the correct answer from @RobJ. He has posted a link to the answer. I am pasting the same answer here as well. The solution is to have default values for the Web API parameters.

public string GetFindBooks(string author="", string title="", string isbn="", string  somethingelse="", DateTime? date= null) 
{
    // ...
}

In my case it will be

public DocumentsDto[] GetDocuments(decimal? PrimaryID = null, decimal? AlternateID = null)
{ ...
like image 93
Sumesh Kuttan Avatar answered Oct 20 '22 18:10

Sumesh Kuttan