Angularjs: $http. Get with null parameters is not hitting the WEB API controller

Source: Internet
Author: User



I am trying to get to the 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 would have the value. So, one of them'll is null always.



My Web API method is


 Public Documentsdto[] GetDocuments (decimaldecimal? ) Alternateid)    {...


When one of the value was null, the URL generated by $http. Get is as follows:


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


Or


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


This does isn't hit my Web API method.



However if I use


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


It can works. I can hardcode the values to null in my params, however I would like to know if there are any correct the-to-achieve this.






Although I specified on my WEB API controller that the both parameters can be null, the ASP. Routing engine would still B e looking for the parameters in a, and even if one of them is null.



Although I can specify which parameter corresponds to the supplied value in the query string, both these methods would have The same signature (a single parameter of typedecimal) in my controller class.



The solution is to has the default values for the Web API parameters.



So, I has one method which takes an object containing one ID set to a value and the other to null, and run my query based On that. as follows:


 Public Documentsdto[] GetDocuments (decimalnulldecimalnull) {...





Angularjs: $http. Get with null parameters is not hitting the WEB API controller


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.