Debugging Swashbuckle Error - Failed to load API Definition

北城余情 提交于 2019-12-23 09:22:15

问题


Is there any way to get a stack trace or inner exceptions on Swashbuckle/Swagger errors? At some point, it stopped working. I'm not sure if it was when I upgraded from .Net Core 2.0 to 2.1, but I'm pretty sure it was still working after that. When I navigate to myapidomain/swagger/index.html I get this error:

Which is not very helpful. It was working 2 or so weeks ago... I didn't change any Swagger configuration. It's the same as it's always been:

public void ConfigureServices(IServiceCollection services)
{
    ...
     services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "My.API",
                Description = "Test"
            });
        });   
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
        app.UseDeveloperExceptionPage();
    else
        app.UseHsts();

    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "PropWorx API V1");
    });
    ...
}

I'm using Swashbuckle.AspNetCore 3.0.0 on an ASP.Net Core 2.1 Web API.


回答1:


Agree that the UI debugging isn't really useful, however the full exception trace can be scraped by opening up your browser's debugging tools (e.g. F12 on Chrome), refreshing your /swagger endpoint, and then examining the swagger.json payload - this is an XHR request which will fail with a 500 status code.

(I would suggest on a big project that you bookmark the link, so that in future you can just go straight to the json file, e.g. https://MyHost/swagger/v1/swagger.json)

e.g. in the below contrived error, I've duplicated the same route between two methods:

[HttpGet]
[Route("parseenum")]
public ActionResult CheckEnum([FromQuery] MyEnum someEnum)
...

[HttpGet]
[Route("parseenum")]
public ActionResult CheckEnum2([FromQuery] MyEnum someEnum)
...

Which produces the error:

Which you should now be able to track down and fix.




回答2:


i have this problem today and try so much to resolve it , you should must remove all [Route] before [http] tag`s in C# controller code like this code for example:

    [Route("~/api/getAll")]
    [HttpGet]
    public ActionResult<List<asteriksModel>>GetAll()
    {
        return _context.asterList.ToList();
    }

and your route code mustbe like this

    [HttpGet]
    public ActionResult<List<asteriksModel>>GetAll()
    {
        return _context.asterList.ToList();
    }

in my project it works fine ,



来源:https://stackoverflow.com/questions/51668952/debugging-swashbuckle-error-failed-to-load-api-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!