I used.Net core API, and installed swagger plug-ins, I configured everything, and able to.
Normal access to the api/swagger address saw the swagger interface, and I started using your redoc to build my API introduction site, but when I introduced my http://{myuri}/swagger.json file, it always burst out
`Something went wrong...
Error downloading http://xxx/swagger/v1/swagger.json Failed to fetch
Stack trace
Error: Error downloading http://xxxxx/swagger/v1/swagger.json
Failed to fetch
at https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js:40:15553
at https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js:118:101808
at
TypeError: Failed to fetch
ReDoc Version: 2.0.0-alpha.17
Commit: de177a0`
I can normally open this http://xxx/swagger/v1/swagger.json address through browser and get content.
But I don't know why, because I'm a swagger document that is automatically generated through the swagger plug-in, so my document is theoretically completely consistent with the OpenAPI protocol.
can you help me
The following is the code of my.Net core related to the use of swagger
`services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "xxx API", Version = "v1" });
//开启swagger对FluentValidation的支持
c.AddFluentValidationRules();
// Set the comments path for the Swagger JSON and UI.
var basePath = AppContext.BaseDirectory;
var xmlPath = Path.Combine(basePath, "xxxx.WebApi.xml");
c.IncludeXmlComments(xmlPath);
//支持bearer token https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/603
c.AddSecurityDefinition(Bearer, new ApiKeyScheme
{
In = "header",
Description = "Please insert JWT with Bearer into field",
Name = "Authorization",
Type = "apiKey"
});
c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
{
{Bearer, new string[] { }}
});
c.OperationFilter<FormFileOperationFilter>();
});`
`// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c => { c.SwaggerEndpoint("../swagger/v1/swagger.json", "My API V1"); });`
Something is wrong with your setup. Maybe CORS is misconfigured.
this is not an issue of redoc
For those people who still cannot make it work, I found the answer at https://stackoverflow.com/questions/50623420/migrating-to-net-core-2-1-breaks-swagger-ui. Hope this help!
I'm seeing the same issue to - I have a working swagger.json file available fpr my site, but trying to plumb it into a local redoc page gives me this error.
Something is wrong with your setup. Maybe CORS is misconfigured.
this is not an issue of redoc
I don't think so as I'm requesting to my local project, It doesn't required CORS configuration at all. And it gives the same error. ASP.NET Core 2.2
Because you named your Swagger Docs "V1", you should add it in app.SwaggerEndpoint() too:
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/V1/swagger.json", "Api"));
In case someone else gets the same error, I cloned an API endpoint and forgot to update the route (had a duplicate) and swagger was giving this _helpful_ error message... corrected the route and the error disappeared.
Because you named your Swagger Docs "V1", you should add it in app.SwaggerEndpoint() too:
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/V1/swagger.json", "Api"));
Seems like I got too used to OpenAPI...I started changing strings around without understanding how everything works together. This worked for me, thanks!
To see the effettive problem try to navigate directly to
https://localhost:port/swagger/v1/swagger.json
and analyze the output.
None of the previous worked for me when i upgraded to swashbuckle 5.4 (and Microsoft.OpenApi to 1.2 preview 3).
What did work was reverting Microsoft.OpenApi back to 1.1.4.
Hope this fix works for you.
According to Microsoft:
To serve the Swagger UI at the app's root (http://localhost:
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});
Be sure you're using [Http] attribute on every endpoint, example:
[HttpDelete("{id}")]
None of the previous worked for me when i upgraded to swashbuckle 5.4 (and Microsoft.OpenApi to 1.2 preview 3).
What did work was reverting Microsoft.OpenApi back to 1.1.4.
Hope this fix works for you.
Yes, I removed Microsoft.OpenApi Reference. and It works for me. I waist a lot times... Finally I got clue with this article.
[SOLVED] The cause might be missing Http Method Annotation in Controller method
To see the effettive problem try to navigate directly to
https://localhost:port/swagger/v1/swagger.json
and analyze the output.
Man, thx so much !
This way i could see what was happening
MissingMethodException: Method not found: 'Void Microsoft.OpenApi.Writers.OpenApiJsonWriter..ctor(System.IO.TextWriter)'.
Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerJson(HttpResponse response, OpenApiDocument swagger)
System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start
System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start
Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.RespondWithSwaggerJson(HttpResponse response, OpenApiDocument swagger)
Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
I had also just encountered this error, it was because I copied a method from another API controller, and forgot to add the Http method and route
This error was a pain to find but eventually it was staring right in my face. Copy and paste this link https://localhost:44353/swagger/v1/swagger.json into the browser and it will show you the exact error you need to fix.
Most helpful comment
Because you named your Swagger Docs "V1", you should add it in app.SwaggerEndpoint() too:
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/V1/swagger.json", "Api"));