Asp.net-Core-Mvc

在 asp .net CORE / MVC 6 應用程序中設置 Swagger 時出現 500 錯誤

  • March 4, 2016

我正在嘗試在新的 asp .net CORE / MVC 6 項目中設置基本的 swagger API 文件,並從 swagger UI 收到 500 錯誤: 500 : http://localhost:4405/swagger/v1/swagger.json

我的啟動類中有以下程式碼:

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 錯誤: Failed to load resource: the server responded with a status of 500 (Internal Server Error)

有什麼辦法可以找出 500 的根本原因,或者在 swagger 中啟用任何額外的調試來找出它為什麼會拋出這個錯誤?根據我看過的一些教程,基本實現只需要我在啟動時擁有的東西。請讓我知道我是否可以提供任何其他資訊。

編輯:這是針對 rc1 的,可能與目前推出的新 netcore 1.0 無關(2016 年 6 月 29 日)

最初我也收到了 500 錯誤。它在堆棧跟踪的深處說:System.NotSupportedException:路徑’api/hotels’的無限HTTP動詞。您是否缺少 HttpMethodAttribute?

事實證明,我的一種 api 方法缺少 HttpGet 屬性:

[Microsoft.AspNetCore.Mvc.HttpGet]

引用自:https://stackoverflow.com/questions/35788911