How to replace Swagger UI header logo in Swashbuckle

前端 未结 7 1338
旧巷少年郎
旧巷少年郎 2020-12-31 09:11

I am using the Swashbuckle package for WebAPI and am attempting to customize the look and feel of the swagger ui default page. I would like to customize the default swagger

相关标签:
7条回答
  • 2020-12-31 09:37

    Here are the steps instead of using the index.html

    Step 1: Create your logo en put it into a folder, in my case I created a separate folder(I am not using the wwwroot) and I named Content. Use StaticFiles for stream content from this folder access via /Content

    app.UseStaticFiles(); // For the wwwroot folder if you need it
    app.UseStaticFiles(new StaticFileOptions()
    {
        FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), "Content")),
        RequestPath = "/Content"
    });
    

    Stem #2: Create an image folder inside Content and place your logo.jpg file. Right-click over the file and change the Build Action to Embedded resource

    Step #3: Create a css folder inside Content and place a new file swagger-mycustom.css and change the Build Action to Content

    On your Startup Configure method add this code

    app.UseSwaggerUI(setupAction =>
    {
        ....
        setupAction.InjectStylesheet("/Content/css/swagger-mycustom.css");
        ...
    }
    

    Step #4: Add this to your css:

    img[alt="Swagger UI"] {
        display: block;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
        content: url('/Content/images/logo.jpg');
        max-width: 100%;
        max-height: 100%;
    }
    

    The output looks like this:

    0 讨论(0)
  • 2020-12-31 09:38

    If you don't want to create the index.html, you can also update the logo with injected css, this is a lot faster than js injection.

    In swagger config enable the c.InjectStylesheet and point to the css you created

    In the css itself:

    .logo__img {
     background: url([PathToLogo]) no-repeat;
     display: block;
     -moz-box-sizing: border-box;
     box-sizing: border-box;
     width: 64px; /* Width of new image */
     height: 25px; /* Height of new image */
     padding-left: 64px; /* Equal to width of new image */
    }
    

    credits for css trick: https://css-tricks.com/replace-the-image-in-an-img-with-css/

    0 讨论(0)
  • 2020-12-31 09:38

    This worked for me with the last version of Swagger UI in Swashbuckle

    .swagger-ui img {
         content: url([logo_path]);
         width: 140px; /* width of your logo */
         height: 40px; /* height of your logo */
    }
    
    0 讨论(0)
  • 2020-12-31 09:45

    I ended up adding a new "index.html" based off this version instead of trying to modify the behavior of the default generated one

    0 讨论(0)
  • 2020-12-31 09:47

    @MichaelD can simply do the following instead:

    .logo__img {
        content: url([PathToLogo]);
        width: 72px; /* Width of new image */
        height: 31px; /* Height of new image */
    }
    
    0 讨论(0)
  • 2020-12-31 09:47

    To replace logo in swagger in api for .net core, you have to add the custom.js file.

    Follow below steps to change logo.

    Step 1 - Create custom.js file and add following code in it

    (function () {
    window.addEventListener("load", function () {
        setTimeout(function () {
    
            var logo = document.getElementsByClassName('link');
    
            logo[0].children[0].alt = "Logo";
            logo[0].children[0].src = "/logo.png";
        });
    }); })();
    

    Step 2 - Inject thar custom.js file in startup.cs file. See below

    app.UseSwaggerUI(c =>
            {
                c.InjectJavascript("/custom.js");
    
            });
    

    Step 3 - Enable static file in the api if not enabled. Add below line of code in Configure method of startup.cs.

    app.UseStaticFiles();
    
    0 讨论(0)
提交回复
热议问题