i'm using swagger ui in my asp.net mvc
Swashbuckle v 5.6
asp.net v 4.8
on local machine everythink working successfully but unfortunately after deploy website to cloud, swagger not load static content, but wen i navigate to these resources using chrome it's loadded successfully
i config swagger using:
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.Schemes(new[] { "http", "https" });
c.IgnoreObsoleteActions();
c.SingleApiVersion("v1", "Console - API");
c.ApiKey("auth")
.Description("API Key Authentication")
.Name("auth")
.In("header");
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
c.IncludeXmlComments(String.Format(#"{0}\bin\docs.xml", System.AppDomain.CurrentDomain.BaseDirectory));
c.UseFullTypeNameInSchemaIds();
})
.EnableSwaggerUi(c =>
{
c.DocumentTitle("Console - API");
c.EnableApiKeySupport("auth", "header");
c.InjectJavaScript(thisAssembly, "Console.API.Assets.Swagger.SwashbuckleCustomAuth.js");
c.InjectStylesheet(thisAssembly, "Console.API.Assets.Swagger.SwashbuckleCustomStyle.css");
});
}
Do i miss any thing? dose https effects swagger load static files?
pss: i don't think this issue related to iis because i have placed a static file in root folder of the website and i was able to load it
The IIS was block simultaneous requests, and the solution was by increment maximum concurrent connections in site advance settings in IIS
Related
I'm trying to read cookie created by Angular app in my ASP.NET Core app. But when I access the cookies from HttpContext I couldn't find it.
C#
var cookie = Request.Cookies["test"]; // always string[0]
services.AddCors(options =>
{
options.AddDefaultPolicy(builder =>
builder.SetIsOriginAllowed(_ => true)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
Angular
setCookie() { this.cookieService.set('test', 'test', 36000000, '/', 'localhost'); }
The client side works very well because I can see the cookie is settled, also the server-side workers because with in the flutter app after adding the domain name it can be accessed.
Can anyone help me?
cookie
I have a blazor wasm application running on a subpath Apps/Genesis/
The problem is some DLL's are not being found:
The request is:
https://localhost:5001/Apps/Genesis/Apps/Genesis/_framework/Blazored.LocalStorage.dll
The request should be:
https://localhost:5001/Apps/Genesis/_framework/Blazored.LocalStorage.dll
app.MapWhen(
context =>
{
return context.Request.Path.StartsWithSegments("/Apps/Genesis");
},
genesis =>
{
// Define blazor files
genesis.UseBlazorFrameworkFiles("/Apps/Genesis");
// Access static files (Ex.: images, dll's) - Used twice because it's on the microsoft docs example
genesis.UseStaticFiles();
genesis.UseStaticFiles("/Apps/Genesis");
// Apply routing for UseEndpoints
genesis.UseRouting();
// Especify Routes
genesis.UseEndpoints(
endpoints =>
{
endpoints.MapControllers();
endpoints.MapFallbackToFile("Apps/Genesis/{{*path:nonfile}}", "Apps/Genesis/index.html");
});
});
Having the below code to configure in Startup.cs
Variables.
private const string SwaggerDocumentVersionName = "v1";
private static string SwaggerDocumentServiceName => $"Users API({SwaggerDocumentVersionName})";
ConfigureServices method.
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(
SwaggerDocumentVersionName,
new OpenApiInfo
{
Title = SwaggerDocumentServiceName,
Version = $"{SwaggerDocumentVersionName}"
});
});
Configure method.
app.UseSwagger(c =>
{
c.RouteTemplate = "/swagger/{documentName}/swagger.json";
});
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "swagger/ui";
c.SwaggerEndpoint($"/swagger/{SwaggerDocumentVersionName}/swagger.json", SwaggerDocumentServiceName);
});
When running locally (https://localhost:5001/swagger/ui resolved to https://localhost:5001/swagger/ui/index.html) definition is loaded correctly and everything seems fine.
Deploying the service to AWS Lambda as ASP.NET Core REST API and navigating to the URL (https://DNS_URL/API_PREFIX/swagger/ui resolved to https://DNS_URL/API_PREFIX/swagger/ui/index.html)it shows the below error loading the JSON definition.
The interesting part is that if you navigate to the JSON definition route (https://DNS_URL/API_PREFIX/swagger/v1/swagger.json) it shows the definition.
The main URL for the API you have released on lambda is https://DNS_URL/API_PREFIX/
Swagger UI needs to fetch the swagger.json file in order for it to work, and for your localhost it is working correctly since https://localhost:5001/swagger/v1/swagger.json is a valid endpoint
(*you have no prefix here)
And the version deployed to lambda is trying to fetch this swagger.json file under
https://DNS_URL/swagger/v1/swagger.json - without your API_PREFIX, thus it's returning 404, not found and swagger ui is displaying the Error message.
Quick fix, which you might apply, that I think would work:
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "swagger/ui";
c.SwaggerEndpoint($"{env.IsDevelopment() ? "" : API_PREFIX}/swagger/{SwaggerDocumentVersionName}/swagger.json", SwaggerDocumentServiceName);
});
Where the API_PREFIX is a string starting with '/'
I'm using ASP.NET Core and NSwag to host and describe a new web service hosted in IIS with Windows Authentication.
Locally I run the web service using https, but when I deploy to a test environment the web service sits behind a load balancer with SSL-offloading. This means that even though the site appears to run under SSL in the browser, the actual binding in IIS is http. So my Swagger UI page (and swagger.json definition) describes the schemes supported as http.
I'd like the Schemes element in the Swagger.json that I use to read "https" instead of "http". Would anyone be able to help me find the property I need to set in my code to set the scheme manually?
{
x-generator: "NSwag v11.19.1.0 (NJsonSchema v9.10.72.0 (Newtonsoft.Json v11.0.0.0))",
swagger: "2.0",
info: {
title: "My API title",
description: "Provides access to data.",
version: "1.0.0"
},
host: "myhostname.net",
schemes: [
"http"
],
etc...
}
Boom. Got it!
Finally found an answer on Github and the following code did the trick:
app.UseSwaggerWithApiExplorer(config =>
{
//...other code omitted...
config.PostProcess = settings =>
{
settings.Schemes.Clear();
settings.Schemes.Add(NSwag.SwaggerSchema.Https);
};
});
EDIT:
for NSwag v12 use:
app.UseSwagger(configure => configure.PostProcess = (document, _) => document.Schemes = new[] { SwaggerSchema.Https });
My project was using NSwag v13 and the below worked for me.
app.UseOpenApi(a => {
a.PostProcess = (document, _) => {
document.Schemes = new[] { OpenApiSchema.Https, OpenApiSchema.Http };
};
});
Source:
https://github.com/RicoSuter/NSwag/issues/1545
I got error when try to config swagger for my new start ASP.NET Core api project. It work perfectly on local host : the comment work good with xml comment. But when I publish it to azure host, it doesn't work.
Then I tried to find out the way is comment the config code which add xml comment to swagger :
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddMvcCore().AddApiExplorer();
services.AddLogging();
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new RequireHttpsAttribute());
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = " API Helper Page",
Description = "A simple start ASP.NET Core Web API/ MBAAS",
TermsOfService = "None",
Contact = new Contact { Name = "Nguyễn Bá Nguyên", Email = "", Url = "https://github.com/hello/" },
License = new License { Name = "Under Construction...", Url = " " }
});
// Set the comments path for the swagger json and ui.
// only working on local, need to be fixed
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var xmlPath = Path.Combine(basePath, ".xml");
c.IncludeXmlComments(xmlPath);
});
}
to get publish to azure without bug I commented the last
//var basePath = PlatformServices.Default.Application.ApplicationBasePath;
//var xmlPath = Path.Combine(basePath, ".xml");
//c.IncludeXmlComments(xmlPath
);
and azure host worked but swagger can't use xml comment :(
So is there any way to config swagger to use xml comment for azure host?
I faced the same issue using swagger xml comments with .NET core on Azure API App. After I set the stdoutLogEnabled flag to true within the web.config I figured out that the XML file is missing. After I uploaded the xml file manually to the API it worked.
To upload the XML file you can use the Kudu service (either type <yourapi>.scm.azurewebsites.net or within the app in the Azure portal -> Development Tools -> Advanced Tools). Then click on Debug console, navigate to your site and upload the xml file: