Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Azure Functions OpenAPI on .NET 5
public static void Main()
{
var host = new HostBuilder()
// πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡ Remove this line below πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡
.ConfigureFunctionsWorkerDefaults()
// πŸ‘†πŸ‘†πŸ‘†πŸ‘†πŸ‘† Remove this line above πŸ‘†πŸ‘†πŸ‘†πŸ‘†πŸ‘†
.Build();
host.Run();
}
public static void Main()
{
var host = new HostBuilder()
// πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡ Add these lines below πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡
.ConfigureFunctionsWorkerDefaults(worker => worker.UseNewtonsoftJson())
.ConfigureOpenApi()
// πŸ‘†πŸ‘†πŸ‘†πŸ‘†πŸ‘† Add these lines above πŸ‘†πŸ‘†πŸ‘†πŸ‘†πŸ‘†
.Build();
host.Run();
}
// πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡ Add OpenAPI related decorators below πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡πŸ‘‡
[OpenApiOperation(operationId: "greeting", tags: new[] { "greeting" }, Summary = "Greetings", Description = "This shows a welcome message.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Summary = "The response", Description = "This returns the response")]
// πŸ‘†πŸ‘†πŸ‘†πŸ‘†πŸ‘† Add OpenAPI related decorators above πŸ‘†πŸ‘†πŸ‘†πŸ‘†πŸ‘†
[Function("Function1")]
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
FunctionContext executionContext)
{
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment