예제 #1
0
        /// <summary>Initializes a new instance of the <see cref="WebApiToSwaggerMiddleware"/> class.</summary>
        /// <param name="nextDelegate">The next delegate.</param>
        public SwaggerDocumentMiddleware(RequestDelegate nextDelegate, IServiceProvider serviceProvider, string documentName, string path, SwaggerDocumentMiddlewareSettings settings)
        {
            _nextDelegate = nextDelegate;

            _documentName = documentName;
            _path         = path;

            _apiDescriptionGroupCollectionProvider = serviceProvider.GetService <IApiDescriptionGroupCollectionProvider>() ??
                                                     throw new InvalidOperationException("API Explorer not registered in DI.");
            _documentProvider = serviceProvider.GetService <SwaggerDocumentProvider>() ??
                                throw new InvalidOperationException("The NSwag DI services are not registered: Call " + nameof(NSwagServiceCollectionExtensions.AddSwaggerDocument) + "() in ConfigureServices().");

            _settings = settings;
        }
예제 #2
0
        /// <summary>
        ///This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            // app.UseAuthentication ();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseStaticFiles();
            app.UseSession();
            // app.UseSpaStaticFiles();
            app.UseCors("AllowAllOrigin");

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });
            var settings = new SwaggerDocumentMiddlewareSettings();

            settings.PostProcess = (document, request) =>
            {
                // document.BaseUrl = "http://192.168.1.99:5000";
                document.Info.Version = "v3";
            };
            // app.UseSwaggerUi3 ();

            app.UseSwagger();

            // app.UseSpa (spa => {
            //     // To learn more about options for serving an Angular SPA from ASP.NET Core,
            //     // see https://go.microsoft.com/fwlink/?linkid=864501
            //     spa.Options.SourcePath = "ClientApp";
            //     if (env.IsDevelopment ()) {
            //         spa.UseAngularCliServer (npmScript: "start");
            //     }
            // });
        }
예제 #3
0
파일: Startup.cs 프로젝트: 24wings/fubang
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseCors("AllowAllOrigin");


            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            var settings = new SwaggerDocumentMiddlewareSettings();

            settings.PostProcess = (document, request) =>
            {
                // document.BaseUrl = "http://192.168.1.99:5000";
                document.Info.Version = "v3";
            };
            // app.UseSwaggerUi3 ();

            app.UseSwagger();

            app.UseMvc();

            // app.UseSpa(spa =>
            // {
            //     // To learn more about options for serving an Angular SPA from ASP.NET Core,
            //     // see https://go.microsoft.com/fwlink/?linkid=864501

            //     spa.Options.SourcePath = "ClientApp";

            //     if (env.IsDevelopment())
            //     {
            //         spa.UseAngularCliServer(npmScript: "hmr");
            //     }
            // });
        }
        private static IApplicationBuilder UseSwaggerWithApiExplorerCore(IApplicationBuilder app, Action <SwaggerDocumentMiddlewareSettings> configure)
        {
            // TODO(v12): Add IOptions support when SwaggerUi3Settings<> T has been removed
            //var settings = configure == null && app.ApplicationServices.GetService<IOptions<SwaggerMiddlewareSettings>>()?.Value ?? new SwaggerMiddlewareSettings();

            var settings = new SwaggerDocumentMiddlewareSettings();

            configure?.Invoke(settings);

            if (settings.Path.Contains("{documentName}"))
            {
                var documents = app.ApplicationServices.GetRequiredService <IEnumerable <SwaggerDocumentRegistration> >();
                foreach (var document in documents)
                {
                    app = app.UseMiddleware <SwaggerDocumentMiddleware>(document.DocumentName, settings.Path.Replace("{documentName}", document.DocumentName), settings);
                }

                return(app);
            }
            else
            {
                return(app.UseMiddleware <SwaggerDocumentMiddleware>(settings.DocumentName, settings.Path, settings));
            }
        }