Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

c# - How can my HTTP triggered Azure Function have the request's parameters directly passed to the Run method?

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)
{
         // ...
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...