Default swagger UI to current instance

Hi wondering if there is any trick to getting /swagger-ui/index.html to reference the “local” /resources url by default rather than the petstore.

ServiceStack.Api.Swagger and ServiceStack.Api.OpenApi open services resources by default. Here is the code in SwaggerFeature which does this. What is your configuration of SwaggerFeature? Did you replace default index.html with custom one?

I did not replace the index.html file… and not setting any configuration…

Plugins.Add(new SwaggerFeature());

This is on dotnetcore if that matters.

For Swagger UI I would recommend to use ServiceStack.Api.OpenApi instead because OpenApi uses more recent version of Swagger UI and Open API specification (formerly named as Swagger), but if you want to use SwaggerFeature, I made a HelloSwagger sample which demonstrates the project with Swagger UI enabled.

To use it you need to run following commands:

git clone https://github.com/xplicit/HelloSwagger
cd HelloSwagger/src/HelloSwagger
dotnet restore
dotnet run 

Then open in browser http://localhost:5000/swagger-ui/ and you will see the list of services (It contains hello endpoint)

If you want to switch to OpenApi later you need to make this changes:

in HelloSwagger.csproj change

<PackageReference Include="ServiceStack.Api.Swagger.Core" Version="1.0.*" />

to

<PackageReference Include="ServiceStack.Api.OpenApi.Core" Version="1.0.*" />

And in HelloSwagger.cs change

Plugins.Add(new SwaggerFeature());

to

Plugins.Add(new OpenApiFeature());

(also you need to change namespaces in usings)

Hi thanks for the example - It does not run in my environment. It gets a 404 on swagger-ui.js and a whole bunch of javascript syntax errors. If i go to index.html under swagger-ui the ui comes up but still defaults to petstore. OpenAPI version doesn’t find /resources.

$ dotnet --info
.NET Command Line Tools (1.0.4)

Product Information:
 Version:            1.0.4
 Commit SHA-1 hash:  af1e6684fd

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.12
 OS Platform: Darwin
 RID:         osx.10.12-x64
 Base Path:   /usr/local/share/dotnet/sdk/1.0.4

I don’t have Mac right now near me to check the sample on OS X, but looks like current directory where the project is running differs from HelloSwagger/src/HelloSwagger.

Are you executing dotnet run from HelloSwagger/src/HelloSwagger directory and then open http://localhost:5000/swagger-ui/ link?

I tested it on Ubuntu 16.04, here is running project http://45.76.85.45:5000/swagger-ui/

I’ve just tried @xplicit’s HelloSwagger in OSX Yosemite using latest .NET Core from https://www.microsoft.com/net/core#macos and the Swagger UI working as expected:

So not sure what problem you’re running into, maybe you can try deleting your NuGet cache?

Just to be clear you’re using the Swagger UI link in the /metadata page right? e.g http://localhost:5000/swagger-ui/

dotnet nuget locals all --clear 

did the trick. Thanks!

(What is strange is this happened on my localhost as well as our kubernetes cluster… but oh well, working now)

1 Like

For posterity’s sake, wanted to reply back that the underlying issue we were having was not using exactly the right url - so if you go to

/swagger-ui  

(no trailing slash) you get a bad page, and if you go to

/swagger-ui/index.html 

it doesn’t re-write the page for your local api.

going to

/swagger-ui/ 

is all good.

1 Like

This is also the URL that is on the metadata page.