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

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.