Reputation: 63
I am creating an Azure API app on Visual Studio 2015. when i hit browse and redirected to http://localhost:3012/
if i add swagger
to the url nothing happens : http://localhost:3012/swagger
it seems I need to add the /docs/v1
for a full address : http://localhost:3012/swagger/docs/v1
. Shouldn't there be like an automatic URL routing when i add /swagger to load swagger page.
Also, i am only able to view the json schema, if i browser to the UI http://localhost:3012/swagger/ui
the page doesn't load.
The API app builds successfully. is there anything missing?
Upvotes: 6
Views: 16744
Reputation: 22847
I had this same problem and was misled a bit by these answers although I saw that they were admittedly entirely correct in the end.
Note the following:
Don't mistake the .EnableSwagger
setting for the EnableSwaggerUi
setting in SwaggerConfig.cs
.
SwaggerConfig.cs
fileThe .EnableSwagger
setting causes raw JSON to be emitted on your application url https://yourapp.azurewebsites.net/swagger/docs/v1
- which is not a great help.
While the .EnableSwaggerUi
setting is what causes the Swagger UI to appear on your url https://yourapp.azurewebsites.net/swagger
Upvotes: 0
Reputation: 3222
In the configuration of your WebAPI project (SwaggerConfig.cs
in the folder App_Start
), you have to enable the Swagger UI. It is commented out as default behavior.
Just open the configuration, search for:
/*
})
.EnableSwaggerUi(c =>
{
*/
and disable the comments in the lines above and under it
Upvotes: 7
Reputation: 464
If you are creating API using Template Azure API App in visual studio, you just need to un-comment few lines from SwaggerConfig.cs file to activate Swagger for your API application.
In your solution look for the file 'SwaggerConfig.cs'
'VSSolution' > App_Start > SwaggerConfig.cs
Look for EnableSwaggerUi and remove comments for below line of code
/*
})
.EnableSwaggerUi(c =>
{
*/
That's it, No Other Configuration Required.
Just run the application : http://localhost:57452/swagger
Upvotes: 2
Reputation: 5432
Pedro, above, has provided a perfect answer for enabling SwaggerUi.
As for your question regarding the URL "swagger/docs/v1
"
This is the default URL used by Swashbuckle to return Swagger 2.0 JSON metadata for the API
The SwaggerConfig.cs
file is created when you install the Swashbuckle package in a project. You can find it in the folder "App_Start" . It provides a number of ways to configure Swashbuckle. I haven't checked if you can change that default URL or do URL rerouting for it.
Edited:
The default route templates for the Swagger docs and swagger-ui are "swagger/docs/{apiVersion}" and "swagger/ui/{*assetPath}" respectively. You can change these so long as the provided templates include the relevant route parameters - {apiVersion} and {*assetPath}. For example: the URL to swagger-ui will be myswag/index.
httpConfiguration .EnableSwagger("docs/{apiVersion}/swagger", c => c.SingleApiVersion("v1", "A title for your API"))
.EnableSwaggerUi("myswag/{*assetPath}");
You can read more about it here in the GitHub repo: https://github.com/domaindrivendev/Swashbuckle
Upvotes: 3