gpt4 book ai didi

c# - 我的 HTTP 触发的 Azure 函数如何将请求的参数直接传递给 Run 方法?

转载 作者:行者123 更新时间:2023-11-30 14:21:48 24 4
gpt4 key购买 nike

我有以下由 HTTP 调用触发的 Azure 函数:

public static class MyAzureFunction
{
[FunctionName("api/v1/resource/")]
public static async Task Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage request, ILogger logger)
{
// Extract query string params from the request...
}
}

我希望将参数自动传递给 Run 方法,就像使用 ASP.NET Core Web API 一样,而不是必须从请求本身中提取它们并解析它们。

这是我想要得到的例子:

[FunctionName("api/v1/resource/{resourceId}")]
public static async Task Run([HttpTrigger(AuthorizationLevel.Function, "get")]HttpRequestMessage request, ILogger logger, int resourceId)
{
// ...
}

或者,在执行 POST 时:

[FunctionName("api/v1/resource/")]
public static async Task Run([HttpTrigger(AuthorizationLevel.Function, "post")]HttpRequestMessage request, ILogger logger, [FromBody] SomeEntityModel entityModel)
{
// ...
}

最佳答案

引用 Azure Functions HTTP triggers and bindings: Customize the HTTP endpoint

对于 GET,您可以使用 Route trigger 上的 attribute 属性为函数设置路由模板

Defines the route template, controlling to which request URLs your function responds. The default value if none is provided is <functionname>

这允许函数代码支持地址中的参数,例如 {resourceId}

您可以将任何Web API 路由约束与您的参数一起使用。

例如

Route = "v1/resource/{resourceId:int}"

By default, all function routes are prefixed with api

下面使用带约束的参数

[FunctionName("MyFunctionName")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/resource/{resourceId:int}")]
HttpRequestMessage request,
ILogger logger,
int resourceId) {
// ...
}

到目前为止,我还没有找到关于 FromBody 的使用细节。属性,但下面的引用似乎富有成效

For C# and F# functions, you can declare the type of your trigger input to be either HttpRequestMessage or a custom type. If you choose HttpRequestMessage, you get full access to the request object. For a custom type, the runtime tries to parse the JSON request body to set the object properties.

注意:强调我的

应该覆盖哪些

[FunctionName("MyPOSTFunction")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1/resource/" )]
SomeEntityModel entityModel,
ILogger logger) {
// ...
}

关于c# - 我的 HTTP 触发的 Azure 函数如何将请求的参数直接传递给 Run 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52748519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com