码迷,mamicode.com
首页 > Windows程序 > 详细

angularjs: $http.get with null parameters are not hitting the Web API controller

时间:2017-03-23 10:38:56      阅读:243      评论:0      收藏:0      [点我收藏+]

标签: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

标签:public   eve   res   ora   doc   app   out   url   class   

原文地址:http://www.cnblogs.com/wxlevel/p/6603799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!