Author Top Nguyen
- Package Manager
PM> Install-Package Elect.Web.Swagger
- .NET CLI
dotnet add package Elect.Web.Swagger
-
Enable generate XML for your project: Edit your
.csproj
add the code below, feel free to change<!-- {namespace} is your root system namespace, eg: Elect.Web --> <PropertyGroup> <!-- Generate XML file --> <DocumentationFile>{namespace}.xml</DocumentationFile> <!-- Disable warning for Controller/Action/Method not have block comment --> <NoWarn>1701;1702;1705;1591</NoWarn> </PropertyGroup> <ItemGroup> <!-- Copy to Ouput --> <Content Include="{namespace}.xml"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup>
-
Add Service
- You can config by parameter
ElectSwaggerOptions
services.AddElectSwagger();
- You can config by parameter
-
Use Middleware
app.UseElectSwagger();
-
Config Display in API Document
- You need specific config for Controller/Action to show in API Document by
ShowInApiDoc
Attribute, if not set, not show in API Document by default. - Use Attribute
ShowInApiDoc
inController
orAction
to EnableAction
show in API Document. - Use Attribute
HideInApiDoc
inController
orAction
to OverrideShowInApiDoc
attribute => makeAction
not show in API Document.- Note: Use specific for each
Action
about Http Method such as[HttpGet]
,[HttpPost]
,[HttpPut]
,[HttpDelete]
and Specific about request path by[Route()]
attribute too.
- Note: Use specific for each
- You need specific config for Controller/Action to show in API Document by
-
Title for API Document
- In your API
Action
need add comment///
to display title for API in Document
- In your API
-
Group Configuration
- Group Name will use the
Controller
Name by default. If you want to change it, useApiDocGroup
to config the group name. - You can config multiple Group Name for
Action
. So theAction
will appear in multiple groups.
- Group Name will use the
-
Additional Parameters for API Document
- You can add additional parameter by
ApiParameter
- Example:
- Add Attribute
[ApiParameter("avatar1", Description = "Avatar File", In = ParameterIn.FormData, Type = ParameterType.File, IsRequire = true)]
. - Then Get Data form Form
var avatarFile1 = HttpContext.Request.Form.Files.GetFile("avatar1")
.
- Add Attribute
- View more in swagger document.
- You can add additional parameter by
-
Access API Document
- By default, API Document URL is "/developers".
- You can config access key to control access permission by the
ElectSwaggerOptions
.
-
Advance Configuration
- If you want to add more config for
SwaggerGenOptions
after Elect.Web.Swagger config it. Just add your config inExtendOptions
Property ofElectSwaggerOptions
.
- If you want to add more config for
Elect.Web.Swagger is licensed under the MIT License.