$ http.get with null parameters does not get into the web API controller

I am trying to connect to a GET Web API controller using the $ http.get app in angular as follows:

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

In my case, the value of PrimaryID or AlternateID will matter. Thus, one of them will always be zero.

My web api method

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

When one of the values ​​is null, the url generated by $ http.get looks like this:

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

or

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

This does not affect my Web API method.

However, if I use

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

it works. I can hard set null values ​​in my parameters, however I would like to know if there is any correct way to achieve this.

Thanks Sam

+4
source share
4 answers

@RobJ. . . , -API.

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

public DocumentsDto[] GetDocuments(decimal? PrimaryID = null, decimal? AlternateID = null)
{ ...
+7

-API, , ASP.NET - , .

: , , , . , , ( decimal) .

, . , , PrimaryID, SecondaryID, , , , , - null, .

+1

[FromUri] Url.

0
source

you can try the following:

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

then you can handle 0 in webapi ...

0
source

Source: https://habr.com/ru/post/1620552/


All Articles