5

I have an Azure Functions app.

I was surprised to learn that when defining a Azure WebJob function the HttpTriggerAttribute does not have to be applied to a HttpRequest parameter.

So while this is valid, and matches most tutorials and examples,

[FunctionName("get")] public async Task<IActionResult> Get( [HttpTrigger(AuthorizationLevel.Anonymous, nameof(HttpMethods.Get), Route = "api/get")]HttpRequest httpRequest) 

This is also valid,

[FunctionName("post")] public async Task<IActionResult> Post( [HttpTrigger(AuthorizationLevel.Anonymous, nameof(HttpMethods.Post), Route = "api/post")]MyType body) 

Since the attribute is not tied to a HttpRequest parameter, I'm surprised the attribute is a parameter attribute and not a method attribute like,

[FunctionName("post")] [HttpTrigger(AuthorizationLevel.Anonymous, nameof(HttpMethods.Post), Route = "api/post")] public async Task<IActionResult> Post(MyType body) 

Why is the HttpTriggerAttribute implemented as a parameter attribute and not a method attribute like FunctionNameAttribute?

1 Answer 1

3

For C # class library development, FunctionName acts as a marker, marking methods as function entry points. It does not involve the input of a function. However, the trigger property is different. It not only has the function of marking the Function type, but also the role of binding input data to method parameters. These data are needed when processing the method. You can think of the trigger property as a binding of the type 'direction in'. This type of binding needs to be entered as a parameter in the development method of the C # class library. Bindings of type 'direction out' are defined outside the function as outputs. Its characteristics are so.

Example of a standard C # class library Function: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-example#class-library-example

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @BowmanZhu. It is helpful to see that this is a convention being followed. But I'm seeking to understand why the convention exists. The explanation I understood from your answer is that this is a parameter property because we're binding to a parameter. But that reasoning seems (to me) not very good when we see that we can be binding to multiple parameters. Example: public async Task<IActionResult> Post([HttpTrigger(...)]MyType body, HttpRequest httpRequest) will bind both the body and httpRequest parameters. But the attribute is applied only to the body parameter.
@Zach My understanding of the relationship between trigger attributes and parameters comes from: learn.microsoft.com/en-us/azure/azure-functions/… In this doc, it says 'binds input data to a method parameter.'. As you said, function only needs to be bound to any parameter declaration. You can understand it as a convention. You can use the trigger attribute to bind any parameter arbitrarily, and you can only set one parameter declaration with trigger attribute.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.