This tutorial instructs you to copy the entire swagger ui (that you already have) to simply add custom css. There is a much easier way to inject your stylesheet, which works like this:
custom.css to it.c.InjectStylesheet("/swagger/custom.css"); to your app.UseSwaggerUI in the Startup.cs file.app.UseStaticFiles(); to your Startup.cs configure method.⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@scottaddie that seems like a better approach.
The above way of customerizing the ui does not work for me. By looking at the inspector by F12 from browser, it shows "Failed to load resource: the server responded with a status of 404 (Not found)
@JinjinM I had the same issue, as you had 404 Not Found. Though honestly I had "caused" it on purpose. The most likely reason of why you are getting this error is that you've forgot to add app.UseStaticFiles(); in Startup.cs configure method.
Oh and by the way this is very nice way of adding custom CSS to Swagger.
@JinjinM I had the same issue, as you had 404 Not Found. Though honestly I had "caused" it on purpose. The most likely reason of why you are getting this error is that you've forgot to add app.UseStaticFiles(); in Startup.cs configure method.
Oh and by the way this is very nice way of adding custom CSS to Swagger.
I have tried add app.UseStaticFiles() in Startup.cs and it worked! Thank you.
How can I run http://localhost:
@jinjinm i had the same issue even i added app.UseStaticFiles still same issue can you anyone help me out. Thank you.
@JinjinM i had the same issue even i added app.UseStaticFiles still same issue can you anyone help me out. Thank you.
@gopaljami would you like to post your code so that i can take a look?
app.UseAuthentication();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.InjectStylesheet("/swagger/ui/custom.css");
});
@gopaljami I am not sure if deleting app.UseDefaultFiles would work. And might better to check your wwwroot folder, does it have a folder "swagger" and a subfolder "ui" with custom.css?
@gopaljami I struggled with this for ages in my .net Core 2.2 project. Whatever I tried I found I was getting a 404 when it came to my custom css file...
I found that in the end I had to use the following when enabling static files:
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "docs")),
RequestPath = "/docs"
});
"docs" is the folder where I was keeping my custom.css. The above code snippet is available from here:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2
then I used the following to enable swaggerUI
// enable the swagger UI
app.UseSwaggerUI(options =>
{
options.RoutePrefix = "docs";
options.SwaggerEndpoint("/swagger/v1/swagger.json", "API Version 1");
options.InjectStylesheet("custom.css");
});
I made sure the "custom.css" was made an embedded resource and suddenly everything sprang to life.
Hope the above is helpful.
Moved to #16195