STEP 1- ADDING SWAGGER (OPEN API INTERFACE)
The first step is to add swagger which is an open API interface and the it could be imported in API management. In order to add swagger first we must install a NuGet package in our existing applications.
STEP 2- EDITING STARTUP CLASS TO ACCOMMIDATE SWAGGER IN OUR APPLICATION
Navigate to the Startup.cs or your Startup class if you have customized that to a different name and change the ConfigureServices and Configure methods to add Swagger to your WebAPI App as the below two methods.
Configure services:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("MyApi", new Info
{
Version = "v1",
Title = "My API",
Description = "A simple example ASP.NET Core Web API",
TermsOfService = "None",
Contact = new Contact
{
Name = "mfouad",
Email = string.Empty,
Url = ""
},
License = new License
{
Name = "Use under LICX",
Url = "https://example.com/license"
}
});
});
}
And Configure:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.UseHttpsRedirection();
app.UseMvc();
}
STEP 3- PUBLISH THESE CHANGES IN THE AZURE APP SERVICE
Right click the project and select publish to add the new changes to the App service.
Select and existing or create a new App service if there is no App Service existing
Publish that to the App Service.
Once published ensure that http://{ YourAppServiceName }.azurewebsites.net/swagger loading the API interface page
Navigate to https://{YourAppServiceName}.azurewebsites.net/swagger/v1/swagger.json
STEP 4- ADD THE APIS TO API MANAGEMENT
- Navigate to the API Management resource in Azure
- Select from the left-hand pane APIs
- Click on OpenAPI
- You will be presented with a form to fill you can either download
the json file to your local machine and validate it using swagger
editor tool if there is any problems importing that file it can give
you suggestions on items to fix before uploading that.
- Add the Swagger.json endpoint or file in the following form and click create.
- Add your API Endpoint to API Management click on the settings from
the ribbon and add your app service URL in the web service URL field
as the below image.
- Test your APIs in API Management by clicking on the Test from the
ribbon and choose one of the APIs to send an example request to as
per the below image.