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

Azure Function中HttpTrigger调用返回404错误问题求助

问题原因分析
  • 核心问题1:HTTP触发方法未添加[FunctionName]特性
    Azure Functions运行时只会将标注了[FunctionName]特性的公共方法识别为独立的函数入口,你目前只有Service Bus触发的Run方法加了该特性,HTTP触发的TestMethodAsync方法未被运行时扫描识别,自然没有对应的端点,访问就返回404。
  • 次要问题:Route配置可能存在路径重复
    Azure Functions默认全局路由前缀为api,如果你在HttpTrigger的Route属性中手动写入api/前缀,最终生成的访问路径会变为/api/api/payments/test-method,和你测试的路径不匹配也会导致404。
修正方案

修正后的完整代码如下:

public static class MyFunction
{
    [FunctionName("MyServiceBusFunction")] // 名称可自定义,只要不重复即可
    public static void Run([ServiceBusTrigger("My-topic", "MySubscription", Connection = "eventbus-connection")]string mySbMsg, ILogger log)
    {
        log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");

        var payment = JsonConvert.DeserializeObject<MyObject>(mySbMsg);
    }

    // 新增FunctionName特性,名称不能和其他函数重复
    [FunctionName("PaymentTestMethod")]
    public static async Task<IActionResult> TestMethodAsync(
        // Route去掉手动加的api前缀,用默认全局前缀拼接即可
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "payments/test-method")]
        HttpRequest req, ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];

        string requestBody = String.Empty;
        using (StreamReader streamReader = new StreamReader(req.Body))
        {
            requestBody = await streamReader.ReadToEndAsync();
        }

        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name ??= data?.name;

        return name != null
            ? (ActionResult) new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
    }
}
验证方法

启动函数项目时,查看控制台输出的可用端点列表,会明确打印出HTTP触发函数的完整访问路径,示例输出如下:

Functions:

        MyServiceBusFunction: serviceBusTrigger

        PaymentTestMethod: [GET,POST] http://localhost:8085/api/payments/test-method

直接访问控制台打印的路径即可正常调用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.15 18:27:05