You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dynamics 365 Business Central自定义API GET请求报BadRequest_MethodNotAllowed错误求助

解决Dynamics 365 Business Central自定义API无法使用GET请求的问题

错误原因解析

你遇到的BadRequest_MethodNotAllowed错误,核心原因是Business Central的API框架默认不允许对「未绑定动作(Unbound Action)」使用GET请求。Action类型的API被设计用于执行操作(如修改数据、触发流程),因此默认仅支持POST方法;而如果是用于查询获取数据的场景,需要将API定义为「未绑定函数(Unbound Function)」,这类API天然支持GET请求,符合REST规范。

具体修改步骤

1. 调整AL代码中的API类型

将代码单元内的方法从[Action]属性改为[Function]属性,并显式指定HttpMethod = 'GET',同时定义返回数据类型:

codeunit 50100 MyCustomDataAPI
{
    // 替换原Action定义为Function,指定GET方法
    [Function(Name = 'RetrieveBusinessData', HttpMethod = 'GET')]
    procedure RetrieveBusinessData(): Text
    begin
        // 你的数据查询逻辑
        exit('Sample data from custom API');
    end;
}

如果需要传递查询参数,可以直接在函数参数中定义,调用时通过URL查询字符串传递:

[Function(Name = 'RetrieveFilteredData', HttpMethod = 'GET')]
procedure RetrieveFilteredData(categoryFilter: Text): List of [Text]
var
    Item: Record Item;
    ResultList: List of [Text];
begin
    Item.SetFilter("Item Category", categoryFilter);
    if Item.FindSet() then repeat
        ResultList.Add(Item.Description);
    until Item.Next() = 0;
    exit(ResultList);
end;

2. 重新发布并确认Web服务

  • 重新发布你的AL扩展
  • 在Business Central的「Web服务」页面,找到新发布的Function类型API,确认其OData URL格式(示例:https://<tenant>.api.businesscentral.dynamics.com/v2.0/<environment>/api/v2.0/companies(<companyId>)/Microsoft.NAV.RetrieveBusinessData)

3. 替代方案:使用API Page查询数据

如果你的需求是返回结构化的集合数据(如查询表记录),更推荐使用Page对象定义API:

page 50100 ItemDataAPI
{
    PageType = API;
    ApplicationArea = All;
    SourceTable = Item;
    ODataKeyFields = "No.";

    layout
    {
        area(content)
        {
            repeater(Items)
            {
                field("No."; "No.") { }
                field(Description; Description) { }
                field(BaseUnitOfMeasure; BaseUnitOfMeasure) { }
            }
        }
    }
}

发布后,直接通过GET请求访问对应的OData URL即可获取数据,还支持OData标准的过滤、分页、排序参数(如?$filter=Description contains 'ABC')。

验证与注意事项

  • 确保Postman中使用的URL与Web服务页面提供的地址完全一致
  • 确认API对应的权限集已分配给当前访问用户
  • 若仍有问题,检查扩展发布后的Web服务注册状态,确保Function类型API已正确加载

内容的提问来源于stack exchange,提问作者RWC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 09:42:39