From 9fe4697b7ecbcd48edfd8cb429a3ef0d463a7260 Mon Sep 17 00:00:00 2001 From: Johannes Date: Fri, 13 Sep 2024 12:41:07 +0200 Subject: [PATCH] SwaggerUI fails when running on IIS/dev-server : Service Unavailable /swagger/v2/swagger.json. However, opening _path_to_app_/swagger/v2/swagger.json in browser produces the json-file. Changing the SwaggerEndpoint to "v2/swagger.json" makes the UI appear, but the urls then used by the "execute"-buttons at the endpoints seems make the requestes at host/controller_route, that is miss the virtual directory. Adding a OpenApiServer seems to fix this. --- PxWeb/Program.cs | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/PxWeb/Program.cs b/PxWeb/Program.cs index 62353ebe..76bf4194 100644 --- a/PxWeb/Program.cs +++ b/PxWeb/Program.cs @@ -106,19 +106,28 @@ public static void Main(string[] args) x.Filters.Add(new LangValidationFilter(langList)) ) .AddNewtonsoftJson(opts => - { - //opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); - opts.SerializerSettings.ContractResolver = new BaseFirstContractResolver(); - opts.SerializerSettings.Converters.Add(new StringEnumConverter { - NamingStrategy = new CamelCaseNamingStrategy() + //opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); + opts.SerializerSettings.ContractResolver = new BaseFirstContractResolver(); + opts.SerializerSettings.Converters.Add(new StringEnumConverter + { + NamingStrategy = new CamelCaseNamingStrategy() + }); + opts.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; + opts.SerializerSettings.DateFormatString = "yyyy-MM-ddTHH:mm:ssZ"; // UTC }); - opts.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; - opts.SerializerSettings.DateFormatString = "yyyy-MM-ddTHH:mm:ssZ"; // UTC - }); + + + // Handle CORS configuration from appsettings.json + bool corsEnbled = builder.Services.ConfigurePxCORS(builder); + + // Bind the configuration to the PxApiConfigurationOptions class + var pxApiConfiguration = new PxApiConfigurationOptions(); + builder.Configuration.Bind("PxApiConfiguration", pxApiConfiguration); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle - builder.Services.AddEndpointsApiExplorer(); + // builder.Services.AddEndpointsApiExplorer(); //only needed for minimal APIS according to + // https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-8.0&tabs=visual-studio builder.Services.AddSwaggerGen(c => { // Sort endpoints @@ -129,15 +138,14 @@ public static void Main(string[] args) Version = "v2-beta" } ); - }); - - // Handle CORS configuration from appsettings.json - bool corsEnbled = builder.Services.ConfigurePxCORS(builder); + c.AddServer(new OpenApiServer + { + Url = (new Uri(pxApiConfiguration.BaseURL)).PathAndQuery, + Description = "API Base URL" + }); - // Bind the configuration to the PxApiConfigurationOptions class - var pxApiConfiguration = new PxApiConfigurationOptions(); - builder.Configuration.Bind("PxApiConfiguration", pxApiConfiguration); + }); var app = builder.Build(); @@ -150,7 +158,7 @@ public static void Main(string[] args) app.UseSwagger(); app.UseSwaggerUI(options => { - options.SwaggerEndpoint("/swagger/v2/swagger.json", "PxWebApi 2.0-beta"); + options.SwaggerEndpoint("v2/swagger.json", "PxWebApi 2.0-beta"); }); }