Reputation: 8382
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)
{
// ...
}
Upvotes: 3
Views: 7025
Reputation: 247153
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 chooseHttpRequestMessage
, 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) {
// ...
}
Upvotes: 7