加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 带有null参数的$http.get没有访问Web API控制器

发布时间:2020-12-17 10:19:56 所属栏目:安全 来源:网络整理
导读:我想在角度应用程序中使用$http.get尝试使用Web API GET控制器,如下所示: $http.get(BasePath + '/api/documentapi/GetDocuments/',{ params: { PrimaryID: ID1,AlternateID: ID2,} }).then( ... 在我的情况下,PrimaryID或AlternateID将具有该值.因此,其中
我想在角度应用程序中使用$http.get尝试使用Web API GET控制器,如下所示:
$http.get(BasePath + '/api/documentapi/GetDocuments/',{
                                    params: {
                                        PrimaryID: ID1,AlternateID: ID2,}
                                }).then( ...

在我的情况下,PrimaryID或AlternateID将具有该值.因此,其中一个将始终为null.

我的Web api方法是

public DocumentsDto[] GetDocuments(decimal? PrimaryID,decimal? AlternateID)
    { ...

当其中一个值为null时,$http.get生成的url如下所示:

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

要么

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

这没有达到我的Web API方法.

但是,如果我使用

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

有用.我可以在我的参数中将值硬编码为null,但是我想知道是否有任何正确的方法来实现这一点.

谢谢,
山姆

我从@RobJ得到了正确答案.他发布了答案的链接.我也在这里粘贴相同的答案.解决方案是具有Web API参数的默认值.
public string GetFindBooks(string author="",string title="",string isbn="",string  somethingelse="",DateTime? date= null) 
{
    // ...
}

在我的情况下,它将是

public DocumentsDto[] GetDocuments(decimal? PrimaryID = null,decimal? AlternateID = null)
{ ...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读