0

Why do functions in AzureFunctions requires to use

[HttpTrigger(AuthorizationLeve.Anonymous, "get")]

Instead of

[HttpTrigger(AuthorizationLeve.Anonymous,  HttpMethods.Get)]

Of course I cannot use

HttpMethods.Get.ToString()

becase language does not allow to call functions in Attributes

and creating my own

public static class HttpMethodsStrings
{
     public const string Get = "get";
}

seems redundant since .NET already provides HttpMethods.

UberFace
  • 445
  • 1
  • 4
  • 14
  • if you think that the response answers your question, please mark it as answer to help other community folks. – Harshita Singh Aug 25 '20 at 10:46
  • 1
    well, it is much likely creating constants, I hoped MS provided the constants somewhere, but seems not, so I was waiting to accept. Thank you – UberFace Aug 25 '20 at 11:38

2 Answers2

1

You can use System.Net.WebRequestMethods.Http.X constants instead of hard coded strings ("get", "post"). I have put together the piece of code and debugged it and it works fine. The snippet of the same is shown below -

enter image description here

Harshita Singh
  • 4,590
  • 1
  • 10
  • 13
1

You can modify your second example to use the nameof operator

eg

[HttpTrigger(AuthorizationLeve.Anonymous,  HttpMethods.Get)]

becomes

[HttpTrigger(AuthorizationLeve.Anonymous,  nameof(HttpMethods.Get))]
Tom Biddulph
  • 514
  • 6
  • 16