標籤:public eve res ora doc app out url class
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/?SecondaryID=154
This does not 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 is any correct way to achieve this.
Although I specified on my 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.
Although I 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 my controller class.
The solution is to have default values for the Web API parameters.
So, I have 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(decimal? PrimaryID = null, decimal? AlternateID = null){ ...
angularjs: $http.get with null parameters are not hitting the Web API controller