在 asp .net CORE/MVC 6 应用程序中设置 Swagger 时出现 500 错误 [英] 500 Error when setting up Swagger in asp .net CORE / MVC 6 app

查看:16
本文介绍了在 asp .net CORE/MVC 6 应用程序中设置 Swagger 时出现 500 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在新的 asp .net CORE/MVC 6 项目中设置基本的 swagger API 文档,并从 swagger UI 收到 500 错误:500 : http://localhost:4405/swagger/v1/swagger.json

I'm trying to setup a basic swagger API doc in a new asp .net CORE / MVC 6 project and receiving a 500 error from the swagger UI: 500 : http://localhost:4405/swagger/v1/swagger.json

我的启动类中有以下代码:

My startup class has the following code in it:

using Swashbuckle.SwaggerGen;
using Swashbuckle.SwaggerGen.XmlComments;
using Swashbuckle.Application;
....
public void ConfigureServices(IServiceCollection services)
{
  ...
  services.AddSwaggerGen();
  services.ConfigureSwaggerDocument(options =>
  {
    options.SingleApiVersion(new Info
    {
        Version = "v1",
        Title = "Blog Test Api",
        Description = "A test API for this blogpost"
    });
  });
}

然后在配置下:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
....
  app.UseSwaggerGen();
  app.UseSwaggerUi();
....
}

当我构建并运行项目时,当我转到 swagger/UI/index.html 时会出现 UI,但会显示上面的 500 错误.当我转到 swagger/v1/swagger.json 链接时,控制台给出以下 500 错误:加载资源失败:服务器响应状态为 500 (Internal Server Error)

When i build and run the project, the UI will come up when i go to swagger/UI/index.html, but the 500 error above is displayed. When i go to the swagger/v1/swagger.json link, console gives the following 500 error: Failed to load resource: the server responded with a status of 500 (Internal Server Error)

有什么方法可以找出 500 的根本原因,或者在 swagger 中启用任何额外的调试来找出它为什么会抛出这个错误?根据我看过的一些教程,基本实现只需要我在启动时拥有的东西.如果我可以提供任何其他信息,请告诉我.

Is there any way i can figure out the root cause of the 500 or enable any additional debug in swagger to figure out why it's throwing this error? Based on some of the tutorials i've looked at, only what i have in startup is required for a base implementation. Please let me know if i can provide any additional information.

这是针对 rc1 的,可能与当前发布的新 netcore 1.0 无关(2016 年 6 月 29 日)

this is for rc1, and may not be relevant to the new netcore 1.0 currently out (6/29/2016)

推荐答案

一开始我也遇到了 500 错误.在堆栈跟踪的深处,它说:System.NotSupportedException:路径 'api/hotels' 的无限 HTTP 动词.是否缺少 HttpMethodAttribute?

Initially I got a 500 error too. Deep down in the stacktrace it said: System.NotSupportedException: Unbounded HTTP verbs for path 'api/hotels'. Are you missing an HttpMethodAttribute?

原来我的 api 方法之一缺少 HttpGet 属性:

It turned out I was missing a HttpGet attribute for one of my api methods:

[Microsoft.AspNetCore.Mvc.HttpGet]

这篇关于在 asp .net CORE/MVC 6 应用程序中设置 Swagger 时出现 500 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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