【问题标题】:In Azure Functions, how to use route parameter inside an output binding在 Azure Functions 中,如何在输出绑定中使用路由参数
【发布时间】:2020-04-22 21:11:05
【问题描述】:

下面是一个http触发函数。对于路线,我指定了一个变量user。现在,我希望在 任何其他 输出绑定中使用这个变量 user现在工作代码以Blob 作为输出绑定显示。

[FunctionName(nameof(Greeting))]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "{user}/greeting")] HttpRequest req,
string user,
[Blob("{user}", Connection = "MyStorageAccount")] CloudBlockBlob blob)
    => new OkResult();

注意我实际上对创建 blob 并不感兴趣。只对有效的语法感兴趣。

编辑:只是意识到这非常有效。

【问题讨论】:

  • 语法没有问题。你有遇到什么问题吗?
  • 谢谢,你是对的。此代码示例已经在工作。我现在发布这个问题的唯一借口是它以前不起作用:p
  • 您好,如果您没有更多问题,您可以标记我的回答来结束这个问题吗?谢谢。:)

标签: c# azure .net-core azure-functions


【解决方案1】:

是的,当然。

看看这个结构:

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 Newtonsoft.Json;

namespace FunctionApp32
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "GetName/{name}")] HttpRequest req,
            string name,
            [Blob("sample-images-sm/{name}", FileAccess.Write)] Stream imageSmall,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

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

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }
}

如果这样写,在本地,如果你发送像http://localhost:7071/api/GetName/Bowman 这样的消息,那么字符串名称将为Bowman,函数将创建一个名为sample-images-sm 的容器,并在此容器下创建一个Bowman 文件。

这是官方文档:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=csharp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 2015-03-27
    • 2020-04-22
    相关资源
    最近更新 更多