65 Followers
·
Follow

Open API(Swagger) and Swagger UI on Azure Functions v2(C#)

I will explain an easy way to provide Open API and Swagger UI in Azure Functions v2.

  1. You install AzureFunctions.Extensions.Swashbuckle package with nuget.
Image for post
Image for post
install nuget package

2. You add startup code on your functions project.

3. You add Open API function and Swagger UI function.

You start DEBUG and access http://localhost:7071/api/swagger/ui on your browser.

Image for post
Image for post
swagger ui

Please add this API code to the project to try it out.

And start DEBUG and access swagger ui.

Image for post
Image for post

Next, The following describes the available Attributes.

ProducesResponseTypeAttribute

If you want to set API result per Http Status, you use to ProducesResponseTypeAttribute.

ProducesResponseTypeAttirbute can multiple set.

Image for post
Image for post

RequestBodyType

If you want to set RequestBody, you use to RequestBodyTypeAttribute.

RequestBodyTypeAttribute must set in HttpRequest or HttpRequestMessage paramater.

RequestModelClass’s property can set data annotation.

Image for post
Image for post

RequestHttpHeaderAttribute

If you want to set Http header, you use to RequestHttpHeaderAttribute.

RequestHttpHeaderAttribute can set multiple, and RequestHttpHeaderAttribute can set Class or Method.

Image for post
Image for post

SwaggerIgnoreAttribute

This package collect all HttpTrigger functions and add in Open API.

If you have Http Trigger Functions that you do not want to add to Open Api, use SwaggerIgnoreAttribute.

Finally, I will introduce how to import XmlCommentDocument and add it to Open API.

If you want to add XML comments in the program code to the OpenAPI Description, do the next:

  1. Add this xml in to functions project’s csproj file.
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DocumentationFile>SampleFunction.xml</DocumentationFile>
</PropertyGroup>

2. Change host.json. You add extensions setting.

{
"version": "2.0",
"extensions": {
"Swashbuckle": {
"XmlPath": "SampleFunction.xml"
}
}
}
Image for post
Image for post
include xml comment

Written by

Microsoft MVP Azure Technologies / https://github.com/yuka1984 / Xamarin / .NET / ASP.NET Core / Web Front End / Azure

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store