Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • duanleou90 14 posts 65 karma points
    Oct 18, 2023 @ 09:37
    duanleou90
    1

    Content Delivery API: Can Not Access Swagger In Production Mode

    Hi everyone,

    I'm playing around with Content Delivery API. As i know, Swagger is disabled when the site is in production mode by default.

    I followed the official document below to make it available always but it didn't work.

    https://docs.umbraco.com/umbraco-cms/reference/api-versioning-and-openapi#swagger-route-and-or-availability

    Do I need to do anything else?

    My code was below:

    In MySwaggerRouteTemplatePipelineFilter.cs

    using Umbraco.Cms.Api.Common.OpenApi;
    using Umbraco.Cms.Web.Common.ApplicationBuilder;
    
    namespace Umbraco12Demo
    {
    
        public class MySwaggerRouteTemplatePipelineFilter : SwaggerRouteTemplatePipelineFilter
        {
            public MySwaggerRouteTemplatePipelineFilter(string name) : base(name)
            {
            }
    
            /// <summary>
            /// This is how you change the route template for the Swagger docs.
            /// </summary>
            protected override string SwaggerRouteTemplate(IApplicationBuilder applicationBuilder) => "swagger/{documentName}/swagger.json";
    
            /// <summary>
            /// This is how you change the route for the Swagger UI.
            /// </summary>
            protected override string SwaggerUiRoutePrefix(IApplicationBuilder applicationBuilder) => "swagger";
    
            /// <summary>
            /// This is how you configure Swagger to be available always.
            /// Please note that this is NOT recommended.
            /// </summary>
            protected override bool SwaggerIsEnabled(IApplicationBuilder applicationBuilder) => true;
        }
    
        public static class MyConfigureSwaggerRouteUmbracoBuilderExtensions
        {
            // call this from ConfigureServices() in Startup, i.e.:
            //     services.AddUmbraco(_env, _config)
            //         ...
            //         .ConfigureMySwaggerRoute()
            //         .Build();
            public static IUmbracoBuilder ConfigureMySwaggerRoute(this IUmbracoBuilder builder)
            {
                builder.Services.Configure<UmbracoPipelineOptions>(options =>
                {
                    //include this line if you do NOT want the Swagger docs at / umbraco / swagger
                    //options.PipelineFilters.RemoveAll(filter => filter is SwaggerRouteTemplatePipelineFilter);
    
                    //setup your own Swagger routes
                    options.AddFilter(new MySwaggerRouteTemplatePipelineFilter("MyApi"));
                });
                return builder;
            }
        }
    
    }
    

    In Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddUmbraco(_env, _config)
            .AddBackOffice()
            .AddWebsite()
            .AddDeliveryApi()
            .AddComposers()
            .ConfigureMySwaggerRoute()
            .Build();
    }
    

    Thanks!

  • Hüseyin 13 posts 104 karma points
    Feb 21, 2024 @ 08:55
    Hüseyin
    0

    yes I had the same problem, but the code worked on my project

    public class SwaggerRouteProductionPipelineFilter() : SwaggerRouteTemplatePipelineFilter("umbraco")
    {
        /// <summary>
        /// This is how you configure Swagger to be available always.
        /// Please note that this is NOT recommended.
        /// </summary>
        protected override bool SwaggerIsEnabled(IApplicationBuilder applicationBuilder) => true;
    }
    
    public static class MyConfigureSwaggerRouteUmbracoBuilderExtensions
    {
        public static IUmbracoBuilder ConfigureProductionSwaggerRoute(this IUmbracoBuilder builder)
        {
            builder.Services.Configure<UmbracoPipelineOptions>(options =>
            {
                options.PipelineFilters.RemoveAll(filter => filter is SwaggerRouteTemplatePipelineFilter);
                options.AddFilter(new SwaggerRouteProductionPipelineFilter());
            });
            return builder;
        }
    }
    

    then in program.cs I've updated it like this:

    builder.CreateUmbracoBuilder()
        .AddBackOffice()
        .AddWebsite()
        .AddDeliveryApi()
        .AddComposers()
    
        //here is the initializer
        .ConfigureProductionSwaggerRoute()
    
        .Build();
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies