This will make your Swagger UI use the host from the incoming request rather than a hardcoded value. Would you like me to explain any part of this in more detail?
User
I use NSwag.AspNetCore 14.4 package.
Assistant
Ah, for NSwag.AspNetCore 14.4, here's how you can set up dynamic host URLs:
csharp
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerDocument(config =>
{
config.PostProcess = document =>
{
document.Info.Title = "Your API";
document.Info.Version = "v1";
};
});
// Add this middleware to handle the dynamic host
services.AddSingleton<IMiddleware>(sp =>
new SwaggerDocumentMiddleware(sp.GetService<IWebHostEnvironment>()));
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Add the OpenAPI/Swagger middleware
app.UseOpenApi(config =>
{
config.PostProcess = (document, request) =>
{
// Clear any existing servers
document.Servers.Clear();
// Add server based on the current request
document.Servers.Add(new OpenApiServer
{
Url = $"{request.Scheme}://{request.Host.Value}"
});
};
});
app.UseSwaggerUi3();
// ... rest of your configuration
}
Use the PostProcess callback in UseOpenApi to modify the OpenAPI document
Clear any existing servers
Add a new server entry with the current request's scheme and host
The Swagger UI will now show the correct host based on how the API is being accessed. Would you like me to explain any part of this solution in more detail?
User
Use AddSwaggerDocument
Assistant
Here's how to set up dynamic host URLs using AddSwaggerDocument with NSwag.AspNetCore 14.4:
csharp
services.AddSwaggerDocument(config =>
{
config.PostProcess = document =>
{
document.Info.Title = "Your API";
document.Info.Version = "v1";
// This will be replaced dynamically
document.Servers.Clear();
};
// Add middleware to modify the document based on the request
config.MiddlewareBasePath = "/api"; // Optional: set if your API has a base path
// Set up dynamic server URL
config.DocumentProcessors.Add(new DocumentProcessor((document, request) =>
{
var currentHost = request.Host.Value;
var currentScheme = request.Scheme;
document.Servers.Clear();
document.Servers.Add(new OpenApiServer
{
Url = $"{currentScheme}://{currentHost}"
});
}));
});
If you need a custom DocumentProcessor, here's the implementation:
csharp
public class DocumentProcessor : IDocumentProcessor
{
private readonly Action<OpenApiDocument, HttpRequest> _processAction;
public DocumentProcessor(Action<OpenApiDocument, HttpRequest> processAction)
{
_processAction = processAction;
}
public void Process(OpenApiDocument document, HttpRequest request)
{
_processAction(document, request);
}
}
You can also use it with more configuration options:
csharp
services.AddSwaggerDocument(config =>
{
config.PostProcess = document =>
{
document.Info.Title = "Your API";
document.Info.Version = "v1";
document.Info.Description = "API Description";
// Optional: Add security definitions if needed
document.SecurityDefinitions.Add("Bearer", new OpenApiSecurityScheme
{
Type = OpenApiSecuritySchemeType.ApiKey,
Name = "Authorization",
In = OpenApiSecurityApiKeyLocation.Header,
Description = "Bearer token"
});
};
config.DocumentProcessors.Add(new DocumentProcessor((document, request) =>
{
var currentHost = request.Host.Value;
var currentScheme = request.Scheme;
document.Servers.Clear();
document.Servers.Add(new OpenApiServer
{
Url = $"{currentScheme}://{currentHost}",
Description = "Dynamic Server URL"
});
}));
});
Make sure you have the necessary using statements:
csharp
using NSwag;
using NSwag.Generation.Processors;
using NSwag.AspNetCore;
This solution will dynamically update the server URL in your Swagger documentation based on the incoming request. Would you like me to explain any part of this in more detail?