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

4

7

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

Zohar answered 17/12, 2015 at 13:7 Comment(4)
please share the web api code at the backend side.Formfitting
This has more to do with the WebApi than Angular, see this postKirstinkirstyn
your parameters are nullable. so you can pass null value. but you need to pass parameter name in url anyhowBarranca
@RobJ Thanks for that post. That solved the problem. Yes, now I see that it is with the WebApi. I will post the same solution here as well as the answer.Zohar
Z
11

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)
{ ...
Zohar answered 18/12, 2015 at 5:32 Comment(0)
M
1

Although you've specified on your Web API controller that the two parameters can be null, the ASP.NET routing engine will still be looking for two parameters in a call to that method - even if one of them is null.

Ideally, you'd create two methods, one which takes just the primary and one just the secondary but in your case this is slightly tricky as both your IDs are of the same type. Although you can specify which parameter corresponds to the supplied value in the query string, both these methods will have the same signature (a single parameter of type decimal) in your controller class.

So you have two options here. Either create new controller so you have one which receives queries for the PrimaryID and one for the SecondaryID, or you have one method which takes an object containing one ID set to a value and the other to null, and run your query based on that.

Matelot answered 17/12, 2015 at 13:14 Comment(0)
R
0

And yet another option can be to convert the request params to a complex object and use [FromUri] to create the object from Url.

Rowe answered 17/12, 2015 at 13:22 Comment(0)
P
0

you can try this:

$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...

Parrakeet answered 18/12, 2015 at 5:38 Comment(1)
This approach is what i was mentioning as hardcoding in the last part of my question. I thought of assigning the value as null itself. Putting an ID to 0 means that the value of the ID is 0. it can be a positive case as well although in general we wont have an ID as 0. Setting it to null is a better approach. Anyways I already have posted the correct answer.Zohar

© 2022 - 2024 — McMap. All rights reserved.