2016-03-04 152 views
11

我試圖在新的asp.net CORE/MVC 6項目中設置一個基本的swagger API文檔,並從swagger用戶界面接收到500錯誤: 500 : http://localhost:4405/swagger/v1/swagger.json在asp.net中設置Swagger時出現錯誤CORE/MVC 6 app

我的啓動類中有下面的代碼:

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

當我建立並運行該項目,該UI會出現,當我去swagger/UI/index.html,但500錯誤上面顯示。當我去招搖/ V1/swagger.json鏈接,控制檯提供了以下500錯誤: Failed to load resource: the server responded with a status of 500 (Internal Server Error)

有什麼辦法,我可以找出500的根本原因或招搖圖使任何額外的調試爲什麼它拋出這個錯誤?根據我所看到的一些教程,只有我在啓動時需要基礎實施。請讓我知道我是否可以提供任何其他信息。

編輯:這是RC1,並可能不相關的新磊目前1.0了(2016年6月29日)

回答

27

最初我得到了一個500錯誤了。在棧跟蹤中,它說: System.NotSupportedException:路徑' api/hotels '的無界HTTP動詞。你是否缺少一個HttpMethodAttribute?

原來我錯過了一個HTTPGET屬性爲我的API方法之一:

[Microsoft.AspNetCore.Mvc.HttpGet] 
+2

我確認了此修復程序。 – mabead

+0

也爲我工作。 –

+0

也爲我工作。 –

-1

給看一下這個項目。 https://github.com/domaindrivendev/Ahoy/tree/master/test/WebSites/Basic

該回購協議從Swashbuckle's所有者,是一個基本的ASP.NET 5示例應用程序,這是幫你糾正配置你的中間件(並採取對他們的訂單照顧,物質it's,如使用 「app.UseSwaggerGen(); app.UseSwaggerUi();後app.UseMvc();)

爲了使你的的applcation記錄給看一下: https://docs.asp.net/en/latest/fundamentals/logging.html?highlight=logging (日誌將內部產生」 的wwwroot 「文件夾

+0

我試圖讓這個項目工作,我真的想添加swashbuckle,但它不會工作。我認爲控制器路由中可能會有某些東西導致它掛掉。繼上述我能夠將它添加到一個新的.net項目沒有問題。我遇到的問題絕對是項目特定的。將標記爲已接受的答案,如果我能弄清楚哪些具體路線導致了問題,我將更新原始問題。謝謝! – CBerg

-2

Swagger的設置因版本而異,這個答案適用於Swashbuckle 6.0.0-beta9和Asp.Net核心1.0。裏面Startup.cs的ConfigureServices方法,你需要添加 -

services.AddSwaggerGen(c => 
    { 
     c.SingleApiVersion(new Info 
     { 
     Version = "v1", 
     Title = "My Awesome Api", 
     Description = "A sample API for prototyping.", 
     TermsOfService = "Some terms ..." 
     }); 
    }); 

然後在配置方法,你必須添加 -

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
    loggerFactory.AddDebug(); 

    app.UseMvc(); 
    app.UseSwaggerGen(); 
    app.UseSwaggerUi(); 
} 

確保您在Startup.cs所引用 -

使用Swashbuckle.SwaggerGen.Generator;

我project.json文件看起來像 -

"dependencies": { 
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final", 
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final", 
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final", 
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final", 
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0-rc2-final", 
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-*", 
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final", 
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final", 
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final", 
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final", 
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final", 
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final", 
    "Swashbuckle": "6.0.0-beta9" 
    }, 

    "tools": { 
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": { 
     "version": "1.0.0-preview1-final", 
     "imports": "portable-net45+win8+dnxcore50" 
    }, 
    "Microsoft.EntityFrameworkCore.Tools": { 
     "version": "1.0.0-preview1-final", 
     "imports": [ 
     "portable-net45+win8+dnxcore50", 
     "portable-net45+win8" 
     ] 
    } 
    }, 

    "frameworks": { 
    "net452": { } 
    }, 
    "buildOptions": { 
    "emitEntryPoint": true, 
    "preserveCompilationContext": true, 
    "xmlDoc": false 
    }, 

    "publishOptions": { 
    "include": [ 
     "wwwroot", 
     "Views", 
     "appsettings.json", 
     "web.config" 
    ] 
    }, 

    "scripts": { 
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] 
    } 
} 
+0

這適用於本地主機。但沒有部署 –

0

另外,如果我可以添加,設置了招搖不喜歡它,當你的根目錄路徑您控制器。例如:

不要這樣做:

[Produces("application/json")] 
[Route("/v1/myController")] 
[Authorize] 
public class myController 
{ 
    [SwaggerResponse((int)System.Net.HttpStatusCode.OK, Type = typeof(RestOkResponse<Response>))] 
    [SwaggerResponse((int)System.Net.HttpStatusCode.InternalServerError, Type = typeof(RestErrorResponse))] 
    [SwaggerResponse((int)System.Net.HttpStatusCode.BadRequest, Type = typeof(RestErrorResponse))] 
    [SwaggerResponse((int)System.Net.HttpStatusCode.Forbidden, Type = typeof(RestErrorResponse))] 
    [SwaggerResponse((int)System.Net.HttpStatusCode.NotFound)] 
    [HttpPost] 
    [Authorize()] 
    public async Task<IActionResult> Create([FromBody] MyObject myObject) 
    { 
     return Ok(); 
    } 
} 

這樣做:

[Produces("application/json")] 
[Authorize] 
public class myController 
{ 
    [SwaggerResponse((int)System.Net.HttpStatusCode.OK, Type = typeof(RestOkResponse<Response>))] 
    [SwaggerResponse((int)System.Net.HttpStatusCode.InternalServerError, Type = typeof(RestErrorResponse))] 
    [SwaggerResponse((int)System.Net.HttpStatusCode.BadRequest, Type = typeof(RestErrorResponse))] 
    [SwaggerResponse((int)System.Net.HttpStatusCode.Forbidden, Type = typeof(RestErrorResponse))] 
    [SwaggerResponse((int)System.Net.HttpStatusCode.NotFound)] 
    [HttpPost("/v1/myController")] 
    [Authorize()] 
    public async Task<IActionResult> Create([FromBody] MyObject myObject) 
    { 
     return Ok(); 
    } 
} 

我花了一段時間來的身影,爲什麼我得到內部服務器錯誤的原因是因爲這個路由問題。希望這可以幫助別人!

2

首先,您可以通過在Configure()上添加app.UseDeveloperExceptionPage();來啓用de開發者例外頁面,以便更好地查看哪些是根本原因。看看here

在我的情況下,問題是我還必須安裝Microsoft.AspNetCore.StaticFiles nuget才能使Swagger工作。

試着卸載/重新安裝Swashbuckle.AspNetCore nuget。

+0

這爲我解決了這個問題 – rfcdejong

+0

卸載/重新安裝Swashbuckle.AspNetCore不適用於我 –