2016-06-28 18 views
7

Ho integrato swagger nella mia applicazione ASP.NET Core RC1 con i seguenti pacchetti NuGet.ASP.NET Core RC1 - Integrazione swagger WebAPI - "Errore" SchemaValidationMessages

"Swashbuckle.SwaggerGen": "6.0.0-rc1-final", 
"Swashbuckle.SwaggerUi": "6.0.0-rc1-final" 

Ecco il codice per l'integrazione swagger.

public void ConfigureServices(IServiceCollection services) 
    { 
     .... 
     ..... 

     //*** Configure Swagger Document Information. 
     services.ConfigureSwaggerDocument(options => 
     { 
      //*** Define incremental API version. 
      options.SingleApiVersion(new Info 
      { 
       Version = "v1", 
       Title = "TEST APIs", 
       Description = "Test API Methods", 
       TermsOfService = "", 
      }); 


      //*** Assign the API - Swagger helper document. 
      options.OperationFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlActionComments(helperDocPath)); 

     }); 

     //*** Configure the Swagger schema settings. 
     services.ConfigureSwaggerSchema(options => 
     { 
      options.DescribeAllEnumsAsStrings = true; 
      options.ModelFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlTypeComments(helperDocPath)); 
     }); 

     .... 
     .... 
    } 

    //**** Configure Method 

    private void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     ... 
     .... 

     //*** Add Swagger pluggins to application environment. 
     app.UseSwaggerGen(); 
     app.UseSwaggerUi(); 
    } 

Il codice genera la documentazione spavalderia mentre si accede localmente usando localhost -> "http://localhost:8080/testapiproject/swagger/ui/index.html".

Tuttavia, dopo la distribuzione del codice del server di distribuzione sto ancora ricevendo il documento spavalderia, ma sto ottenendo "errore" nella parte inferiore, su di clic su di esso dice,

{"schemaValidationMessages":[{"level":"error","message":"Can't read from file http://<applicationdomainname>:8080//testapiproject/swagger/v1/swagger.json"}]}. 
+0

'http: //: 8080 // testapiproject/swagger/v1/swagger.json' questo percorso sembra essere sbagliato. 'hostname' è mancante. Stai cercando di ottenere json da un dominio sbagliato. – Venky

+0

Qualsiasi cosa diversa da localhost, il suo errore di visualizzazione nella parte inferiore che è un errore di convalida dello schema. – JAK

risposta

5

penso che sta cercando di accedere ai dati json da un percorso errato. Assicurarsi che il percorso sia configurato correttamente nel testapiproject/swagger/ui/index.html" di swaggerui

$(function() { 
     var url = window.location.search.match(/url=([^&]+)/); 
     if (url && url.length > 1) { 
      url = decodeURIComponent(url[1]); 
     } else { 
      url = "/swagger/docs/v1";  // Make sure it's not hardcoded 
     } 
} 

aggiornato per Net Nucleo

ho installato solo "Swashbuckle": "6.0.0-beta902" pacchetto.

public void ConfigureServices(IServiceCollection services) 
{ 
      services.AddSwaggerGen(); 
      services.ConfigureSwaggerGen(options => 
      { 
       options.SingleApiVersion(new Info 
       { 
        Version = "v1", 
        Title = "test API", 
        Description = "test api for swagger", 
        TermsOfService = "None", 

       }); 
       options.IncludeXmlComments(/* whatever tha path */); 
       options.DescribeAllEnumsAsStrings(); 
      }); 

    } 

Nel mehod configurre aggiunto solo

  app.UseSwagger(); 
      app.UseSwaggerUi(); // inside this method we can pass the root path of swagger 

Si stava lavorando con localhost ma non nella directory Vitural. Quindi ho dovuto aggiungere la configurazione di seguito a web.config e poi ha iniziato a funzionare.

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true" /> 
</system.webServer> 
+1

Non riesco a farlo poiché utilizzo l'integrazione swagger per il progetto webAPi di ASP.NET Core RC1. In RC1, non puoi trovare index.html. – JAK

+0

stesso problema di @JAK. È virtuale –

1

Swager genera documenti API dai commenti di codice che vengono archiviati in file xml. Ad esempio, i commenti per GatewayServer.dll verranno archiviati in GatewayServer.xml. È necessario caricarne il server di distribuzione.

Problemi correlati