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

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

问题描述

我通过HTTP调用触发了以下Azure函数:

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

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

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)
{
     // ...
} 

或者,在进行POST时:

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)
{
         // ...
}

推荐答案

参考对于GET,您可以在触发器上使用Route属性属性来设置该功能的路由模板

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

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

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

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

您可以在参数中使用任何 Web API路由约束.

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

例如

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

默认情况下,所有功能路由都以 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) {
    // ...
}

到目前为止,我还无法找到有关FromBody属性使用的详细信息,但是以下引用似乎很有用

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

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

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

应涵盖的范围

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

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

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