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

yu_ka1984
3 min readApr 15, 2019

--

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.
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.

swagger ui

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

And start DEBUG and access swagger ui.

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.

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.

RequestHttpHeaderAttribute

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

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

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"
}
}
}
include xml comment

--

--

yu_ka1984

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