2016-03-04 5 views
11

Sto provando a impostare un doc di base swagger API in un nuovo progetto ASP .net CORE/MVC 6 e a ricevere un errore 500 dall'interfaccia utente swagger: 500 : http://localhost:4405/swagger/v1/swagger.jsonErrore 500 durante l'impostazione di Swagger nell'app. CORE/MVC 6 asp

la mia classe di avvio ha il seguente codice in esso:

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

e poi in Configura:

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

quando costruire ed eseguire il progetto, l'interfaccia utente verrà fuori quando vado a swagger/UI/index.html, ma viene visualizzato l'errore 500 sopra. Quando vado al/v1/link swagger.json spavalderia, console dà il 500 errore seguente: Failed to load resource: the server responded with a status of 500 (Internal Server Error)

C'è un modo riesco a capire la causa principale del 500 o permettere a qualsiasi supplementari di debug in spavalderia di figura perché sta generando questo errore? Sulla base di alcune delle esercitazioni che ho esaminato, è necessario solo ciò che ho in avvio per un'implementazione di base. Per favore fatemi sapere se posso fornire ulteriori informazioni.

EDIT: questo è per RC1, e non può essere rilevante per il nuovo netcore 1,0 attualmente fuori (6/29/2016)

risposta

27

Inizialmente ho ottenuto un errore 500 anche. Nel profondo dello stacktrace si diceva: System.NotSupportedException: verbi HTTP illimitati per il percorso ' api/hotels '. Ti manca un HttpMethodAttribute?

Si è scoperto che mi mancava un attributo HttpGet per uno dei miei metodi API:

[Microsoft.AspNetCore.Mvc.HttpGet] 
+2

Confermo questa correzione. – mabead

+0

Ha funzionato anche per me. –

+0

Ha funzionato anche per me. –

-1

Dare un'occhiata a questo progetto. https://github.com/domaindrivendev/Ahoy/tree/master/test/WebSites/Basic

Questa pronti contro termine è da titolare Swashbuckle's, è un semplice ASP.NET 5 Sample app, questo è che si aiutano a correggere configurare il vostro middleware (e prendersi cura di ordini del loro, vale la materia, ad esempio, , utilizzare "app.UseSwaggerGen(); app.UseSwaggerUi(); dopo app.UseMvc();)

Per attivare la registrazione nel vostro applcation dare un'occhiata a: https://docs.asp.net/en/latest/fundamentals/logging.html?highlight=logging (il registro verrà generato all'interno" wwwroot "cartella

+0

Ho provato a farlo funzionare con il progetto volevo davvero aggiungere lo swashbuckle ma non funzionerà. Penso che ci possa essere qualcosa nelle rotte del controller che sta causando il blocco. Seguendo quanto sopra ho potuto aggiungerlo a un nuovo progetto .net senza problemi. Il problema che sto incontrando è sicuramente specifico del progetto. Contrassegnerà come risposta accettata e se riuscirò mai a capire quali percorsi specifici stanno causando il problema, aggiornerò la domanda originale. Grazie! – CBerg

-2

La configurazione di Swagger varia notevolmente da versione a versione. Questa risposta è per Swashbuckle 6.0.0-beta9 e Asp.Net Core 1.0. All'interno del metodo ConfigureServices di Startup.cs, è necessario aggiungere -

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

Poi nel metodo di configurazione è necessario aggiungere -

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

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

Assicurarsi che si fa riferimento in Startup.cs -

utilizzando Swashbuckle.SwaggerGen.Generator;

Il mio file project.json assomiglia -

"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

Funziona sull'host locale. ma non al momento dell'implementazione –

0

Anche se posso aggiungere, la spavalderia istituito non piace quando si tratta al livello principale della vostra controllori.Per esempio:

non farlo:

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

fare questo:

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

Mi c'è voluto un po 'per capire che la ragione per cui mi stavo errore interno del server è stato a causa di questo problema di routing. Spero che questo aiuti qualcuno!

2

Innanzitutto è possibile abilitare la pagina delle eccezioni di sviluppatore aggiungendo app.UseDeveloperExceptionPage(); in Configure() per vedere meglio quale è la causa principale. Date un'occhiata here

Nel mio caso il problema era che devo installare anche Microsoft.AspNetCore.StaticFiles nuget per far funzionare Swagger.

Provare anche a disinstallare/reinstallare Swashbuckle.AspNetCore nuget.

+0

Questo lo ha risolto per me – rfcdejong

+0

uninstall/reinstall Swashbuckle.AspNetCore non funziona per me –

Problemi correlati