我的 HTTP 触发 Azure 函数如何将请求的参数直接传递给 Run 方法? [英] How can my HTTP triggered Azure Function have the request's parameters directly passed to the Run method?

查看:18
本文介绍了我的 HTTP 触发 Azure 函数如何将请求的参数直接传递给 Run 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

公共静态类 MyAzureFunction{[FunctionName("api/v1/resource/")]公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function,get")]HttpRequestMessage 请求,ILogger 记录器){//从请求中提取查询字符串参数...}}

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

这是我想要得到的示例:

[FunctionName("api/v1/resource/{resourceId}")]公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function,get")]HttpRequestMessage 请求,ILogger 记录器,int resourceId){//...}

或者,在进行 POST 时:

[FunctionName("api/v1/resource/")]公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function,post")]HttpRequestMessage 请求,ILogger 记录器,[FromBody] SomeEntityModel entityModel){//...}

解决方案

参考 Azure Functions HTTP 触发器和绑定:自定义 HTTP 终结点

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

<块引用>

定义路由模板,控制您的函数响应的请求 URL.如果没有提供,则默认值为 <functionname>

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

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

例如

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

<块引用>

默认情况下,所有函数路由都以前缀api

以下使用带约束的参数

[FunctionName("MyFunctionName")]公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/resource/{resourceId:int}")]HttpRequestMessage 请求,ILogger 记录器,诠释资源ID){//...}

到目前为止,我还没有找到关于使用 FromBody 属性的详细信息,但是下面的引用似乎很有成效

<块引用>

对于 C# 和 F# 函数,您可以将触发器输入的类型声明为 HttpRequestMessage 或自定义类型.如果您选择 HttpRequestMessage,您将获得对请求对象的完全访问权限.对于自定义类型,运行时会尝试解析 JSON 请求正文以设置对象属性.

注意:强调我的

应该涵盖哪些

[FunctionName("MyPOSTFunction")]公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1/resource/" )]SomeEntityModel 实体模型,ILogger 记录器){//...}

I have the following Azure Function triggered by an HTTP call:

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...
    } 
}

I would like to have the parameters to be automatically passed to the Run method, the same way it is being done with ASP.NET Core Web API, instead of having to extract them from the request itself and parse them.

Here is an example of what I would like to get:

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

Or, when doing a POST:

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

解决方案

Reference Azure Functions HTTP triggers and bindings: Customize the HTTP endpoint

For the GET you can use the Route attribute property on the trigger to set a route template for the function

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

This allows the function code to support parameters in the address, like {resourceId}.

You can use any Web API Route Constraint with your parameters.

for example

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

By default, all function routes are prefixed with api

The following makes use of the parameter with constraints

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

So far I have not been able to find details about the use of FromBody attribute, but the following quote seems fruitful

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.

note: emphasis mine

Which should cover

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

这篇关于我的 HTTP 触发 Azure 函数如何将请求的参数直接传递给 Run 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆