我已经在我的ASP.NET Core RC1应用程序中使用以下NuGet包集成了swagger.
"Swashbuckle.SwaggerGen": "6.0.0-rc1-final","Swashbuckle.SwaggerUi": "6.0.0-rc1-final"
这是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(); }
该代码使用localhost在本地访问时生成swagger文档 – > “http://localhost:8080/testapiproject/swagger/ui/index.html”.
但是,在部署服务器中部署代码之后,我仍然得到了swagger文档但是我在底部得到“错误”,点击它后说,
{"schemaValidationMessages":[{"level":"error","message":"Can't read from file http://<applicationdomainname>:8080//testapiproject/swagger/v1/swagger.json"}]}.
解决方法
我认为它试图从错误的路径访问json数据.确保在swaggerui的testapiproject / swagger / ui / index.html中正确配置路径
$(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 } }
更新.Net Core
我只安装了“Swashbuckle”:“6.0.0-beta902”包.
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(); }); }
在configurre mehod添加了
app.UseSwagger(); app.UseSwaggerUi(); // inside this method we can pass the root path of swagger
它与localhost一起使用,但不在vitural目录中.所以我不得不将以下配置添加到web.config然后它开始工作.
<system.webServer> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer>