angularjs: $http.get with null parameters are not hitting the Web API controller
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了angularjs: $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/?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的主要内容,如果未能解决你的问题,请参考以下文章
AngularJS:$http.get 405(不允许的方法)
如何在 AngularJS 中从 $http.get 返回图像
angularJS中的$http.get错误——成功不是函数[重复]