调试 Swashbuckle 错误 - 无法加载 API 定义 [英] Debugging Swashbuckle Error - Failed to load API Definition

查看:16
本文介绍了调试 Swashbuckle 错误 - 无法加载 API 定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获得堆栈跟踪或 Swashbuckle/Swagger 错误的内部异常?在某些时候,它停止了工作.我不确定是不是在我从 .Net Core 2.0 升级到 2.1 时,但我很确定它在那之后仍然可以工作.当我导航到 myapidomain/swagger/index.html 时,我收到此错误:

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:

这不是很有帮助.大约两周前它还在工作……我没有更改任何 Swagger 配置.一如既往:

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");
    });
    ...
}

我在 ASP.Net Core 2.1 Web API 上使用 Swashbuckle.AspNetCore 3.0.0.

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

推荐答案

同意 UI 调试并不是很有用,但是可以通过打开浏览器的调试工具(例如 Chrome 上的 F12)来抓取完整的异常跟踪,刷新您的 /swagger 端点,然后检查 swagger.json 有效负载 - 这是一个 XHR 请求,将失败并显示 500 状态代码.

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.

(我建议在一个大项目上为链接添加书签,以便将来您可以直接转到 json 文件,例如 https://MyHost/swagger/v1/swagger.json)

(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 you should now be able to track down and fix.

这篇关于调试 Swashbuckle 错误 - 无法加载 API 定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆