You need to enable JavaScript to run this app. 如何使用单个Azure Function实现多HTTP重定向配置需求
优惠活动
大模型
产品
解决方案
定价
更多

如何使用单个Azure Function实现多HTTP重定向配置需求

问题1解答:单个Azure Function完全可以实现多HTTP重定向需求

你完全可以用单个HTTP触发型Azure Function满足需求,哪怕后续要维护超过100个重定向规则也完全没有性能或功能限制。更推荐你把重定向规则存到Azure Function的应用配置里,不用硬编码到代码中,后续新增/修改规则不需要重新发布代码,直接改配置即可。

问题2解答:示例代码(以C#进程内模型为例)

前置配置规则

先在Azure Function管理后台的「配置」-「应用程序设置」中添加所有重定向规则,命名格式统一为RedirectRule_{输入值},默认兜底规则命名为RedirectRule_Default,示例配置如下:

  • RedirectRule_A = LinkedIn对应地址
  • RedirectRule_B = Xing对应地址
  • RedirectRule_C = Power Automate对应地址
  • RedirectRule_D = Power Apps对应地址
  • RedirectRule_Default = Google对应地址

后续新增100+规则时,直接在应用配置里按上述格式添加即可,不需要修改代码。

函数代码

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

public class RedirectFunction
{
    private readonly IConfiguration _configuration;

    // 注入配置服务,直接读取应用配置中的重定向规则
    public RedirectFunction(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    [FunctionName("CustomRedirect")]
    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        // 优先读Query参数的input,其次读请求Body的input参数
        string input = req.Query["input"];
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        input = input ?? data?.input;

        // 匹配对应重定向规则
        string configKey = string.IsNullOrEmpty(input) ? "RedirectRule_Default" : $"RedirectRule_{input.Trim().ToUpper()}";
        string targetUrl = _configuration[configKey];

        // 无匹配规则时跳兜底地址
        if (string.IsNullOrEmpty(targetUrl))
        {
            targetUrl = _configuration["RedirectRule_Default"];
        }

        // 返回302临时重定向,需要永久重定向可修改permanent参数为true
        return new RedirectResult(targetUrl, permanent: false);
    }
}

调用示例

访问 https://<你的函数实例名>.azurewebsites.net/api/CustomRedirect?input=A 即可触发重定向到LinkedIn地址,不带input参数时自动跳转到默认的Google地址。

如果使用Python/Node.js等其他语言开发Azure Function,逻辑完全一致:读取输入参数 -> 匹配重定向规则 -> 返回重定向响应即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 15:06:11