.NET Core 2 和 SwashBuckle Swagger UI 未显示
.NET Core 2 and SwashBuckle Swagger UI is not Displaying
我已经学习了一些教程并在工作中使用了它,但由于某种原因我无法显示 UI,但创建了 Swagger Json。我最后看的教程是 here.
我的设置是这样的:
Nuget 包: Swashbuckle.AspNetCore(1.0.0)
ConfigureServices
方法:
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1",
new Info
{
Title = "MediatR Example",
Version = "v1",
Description = "Trying out the MediatR library to simplify Request and Response logic.",
TermsOfService = "WTFPL",
Contact = new Contact
{
Email = "",
Name = "",
Url = "https://github.com/CubicleJockey/MediatR-Playground"
}
}
);
var xmlDocFile = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, @"MediatR-Messages.Api.xml");
options.IncludeXmlComments(xmlDocFile);
options.DescribeAllEnumsAsStrings();
});
Configure
方法:
app.UseMvcWithDefaultRoute();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint
app.UseSwaggerUI(config =>
{
config.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
});
launchSettings.json
:
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
运行 并访问 Swagger JSON url 产生适当的 JSON:
{
"swagger":"2.0",
"info":{
"version":"v1",
"title":"MediatR Example",
"description":"Trying out the MediatR library to simplify Request and Response logic.",
"termsOfService":"WTFPL",
"contact":{
"name":"André Davis",
"url":"https://github.com/CubicleJockey/MediatR-Playground",
"email":"davis.andre@gmail.com"
}
},
"basePath":"/",
"paths":{
"/api/Addition":{
"get":{
"tags":[
"Addition"
],
"summary":"Get Methods that takes two numbers and gets the sum.",
"operationId":"ApiAdditionGet",
"consumes":[
],
"produces":[
"text/plain",
"application/json",
"text/json"
],
"parameters":[
{
"name":"left",
"in":"query",
"description":"Left hand side of the equation.",
"required":false,
"type":"integer",
"format":"int32"
},
{
"name":"right",
"in":"query",
"description":"Right hand side of the equation.",
"required":false,
"type":"integer",
"format":"int32"
}
],
"responses":{
"200":{
"description":"Success",
"schema":{
"$ref":"#/definitions/Task[AdditionResponse]"
}
}
}
}
}
},
"definitions":{
"Task[AdditionResponse]":{
"type":"object",
"properties":{
"result":{
"$ref":"#/definitions/AdditionResponse",
"readOnly":true
},
"id":{
"format":"int32",
"type":"integer",
"readOnly":true
},
"exception":{
"type":"object",
"readOnly":true
},
"status":{
"enum":[
"Created",
"WaitingForActivation",
"WaitingToRun",
"Running",
"WaitingForChildrenToComplete",
"RanToCompletion",
"Canceled",
"Faulted"
],
"type":"string",
"readOnly":true
},
"isCanceled":{
"type":"boolean",
"readOnly":true
},
"isCompleted":{
"type":"boolean",
"readOnly":true
},
"isCompletedSuccessfully":{
"type":"boolean",
"readOnly":true
},
"creationOptions":{
"enum":[
"None",
"PreferFairness",
"LongRunning",
"AttachedToParent",
"DenyChildAttach",
"HideScheduler",
"RunContinuationsAsynchronously"
],
"type":"string",
"readOnly":true
},
"asyncState":{
"type":"object",
"readOnly":true
},
"isFaulted":{
"type":"boolean",
"readOnly":true
}
}
},
"AdditionResponse":{
"type":"object",
"properties":{
"answer":{
"format":"int32",
"type":"integer",
"readOnly":true
},
"equation":{
"type":"string",
"readOnly":true
}
}
}
},
"securityDefinitions":{
}
}
当访问默认的 Swagger UI url 我得到一个 404。尝试了一些变化。
- localhost:64881/招摇/
- localhost:64881/swagger/ui
- localhost:64881/swagger/index.html
- localhost:64881/swagger/ui/index.html
以上所有return 404。根据版本的不同,这些之前都有效。我错过了什么。
我的完整源代码可以在 GitHub here 上找到。这是这个问题的一个分支,所以代码符合我的要求。
下载并测试您的代码后,您似乎必须将以下 NuGet 包添加到您的项目中:
Microsoft.AspNetCore.StaticFiles
您可以通过 NuGet 管理器或将以下行添加到您的 .csproj <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
来源:https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/438
对我有什么用处:
- 从部署文件夹中删除所有旧文件(我尝试使用
IIS
,也适用于其他托管类型)
- 将所有
Microsoft.AspNetCore.*
包替换为 Microsoft.AspNetCore.All
元包 - 有关详细信息,请参阅 this post。
- [可选] 只是为了防止副作用,重新安装
Swashbuckle.AspNetCore
包(不需要其他 Swashbuckle.AspNetCore.*
包)
确保项目文件中有这两个包(足以使其工作):
PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0"
PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0"
发布(或通过复制文件进行部署)到您的部署文件夹。
现在它应该工作了。
注意:如果您的 API 中有重复的型号名称,有时它会失败(在这种情况下,它会在浏览器中显示一些不清楚的错误);)
尝试删除 .vs
文件夹。在升级到 ASP.NET Core 2.1
后为我修复了它
我几个小时都遇到了同样的问题,但使用的端口被 Chrome 认为是不安全的(说明这一点的文本是谨慎的,很容易被忽略)。
这可能对将来的某些人有用。
我有同样的问题,我缺少 HttpMethod 绑定。
System.NotSupportedException:操作需要 Swagger 2.0 的显式 HttpMethod 绑定
重新清理并构建您的项目。
此外,请确保控制器中的所有方法都具有 ActionVerbs,即:HttpGet, HttpPost
等等,具有 [ApiExplorerSettings(IgnoreApi = true)]
属性的方法除外。
ActionVerb 和 Route 必须分开:
执行 [HttpGet,Route("getuser")]
而不是 [HttpGet("getuser")]
我已经学习了一些教程并在工作中使用了它,但由于某种原因我无法显示 UI,但创建了 Swagger Json。我最后看的教程是 here.
我的设置是这样的:
Nuget 包: Swashbuckle.AspNetCore(1.0.0)
ConfigureServices
方法:
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1",
new Info
{
Title = "MediatR Example",
Version = "v1",
Description = "Trying out the MediatR library to simplify Request and Response logic.",
TermsOfService = "WTFPL",
Contact = new Contact
{
Email = "",
Name = "",
Url = "https://github.com/CubicleJockey/MediatR-Playground"
}
}
);
var xmlDocFile = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, @"MediatR-Messages.Api.xml");
options.IncludeXmlComments(xmlDocFile);
options.DescribeAllEnumsAsStrings();
});
Configure
方法:
app.UseMvcWithDefaultRoute();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint
app.UseSwaggerUI(config =>
{
config.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
});
launchSettings.json
:
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
运行 并访问 Swagger JSON url 产生适当的 JSON:
{
"swagger":"2.0",
"info":{
"version":"v1",
"title":"MediatR Example",
"description":"Trying out the MediatR library to simplify Request and Response logic.",
"termsOfService":"WTFPL",
"contact":{
"name":"André Davis",
"url":"https://github.com/CubicleJockey/MediatR-Playground",
"email":"davis.andre@gmail.com"
}
},
"basePath":"/",
"paths":{
"/api/Addition":{
"get":{
"tags":[
"Addition"
],
"summary":"Get Methods that takes two numbers and gets the sum.",
"operationId":"ApiAdditionGet",
"consumes":[
],
"produces":[
"text/plain",
"application/json",
"text/json"
],
"parameters":[
{
"name":"left",
"in":"query",
"description":"Left hand side of the equation.",
"required":false,
"type":"integer",
"format":"int32"
},
{
"name":"right",
"in":"query",
"description":"Right hand side of the equation.",
"required":false,
"type":"integer",
"format":"int32"
}
],
"responses":{
"200":{
"description":"Success",
"schema":{
"$ref":"#/definitions/Task[AdditionResponse]"
}
}
}
}
}
},
"definitions":{
"Task[AdditionResponse]":{
"type":"object",
"properties":{
"result":{
"$ref":"#/definitions/AdditionResponse",
"readOnly":true
},
"id":{
"format":"int32",
"type":"integer",
"readOnly":true
},
"exception":{
"type":"object",
"readOnly":true
},
"status":{
"enum":[
"Created",
"WaitingForActivation",
"WaitingToRun",
"Running",
"WaitingForChildrenToComplete",
"RanToCompletion",
"Canceled",
"Faulted"
],
"type":"string",
"readOnly":true
},
"isCanceled":{
"type":"boolean",
"readOnly":true
},
"isCompleted":{
"type":"boolean",
"readOnly":true
},
"isCompletedSuccessfully":{
"type":"boolean",
"readOnly":true
},
"creationOptions":{
"enum":[
"None",
"PreferFairness",
"LongRunning",
"AttachedToParent",
"DenyChildAttach",
"HideScheduler",
"RunContinuationsAsynchronously"
],
"type":"string",
"readOnly":true
},
"asyncState":{
"type":"object",
"readOnly":true
},
"isFaulted":{
"type":"boolean",
"readOnly":true
}
}
},
"AdditionResponse":{
"type":"object",
"properties":{
"answer":{
"format":"int32",
"type":"integer",
"readOnly":true
},
"equation":{
"type":"string",
"readOnly":true
}
}
}
},
"securityDefinitions":{
}
}
当访问默认的 Swagger UI url 我得到一个 404。尝试了一些变化。
- localhost:64881/招摇/
- localhost:64881/swagger/ui
- localhost:64881/swagger/index.html
- localhost:64881/swagger/ui/index.html
以上所有return 404。根据版本的不同,这些之前都有效。我错过了什么。
我的完整源代码可以在 GitHub here 上找到。这是这个问题的一个分支,所以代码符合我的要求。
下载并测试您的代码后,您似乎必须将以下 NuGet 包添加到您的项目中:
Microsoft.AspNetCore.StaticFiles
您可以通过 NuGet 管理器或将以下行添加到您的 .csproj <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
来源:https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/438
对我有什么用处:
- 从部署文件夹中删除所有旧文件(我尝试使用
IIS
,也适用于其他托管类型) - 将所有
Microsoft.AspNetCore.*
包替换为Microsoft.AspNetCore.All
元包 - 有关详细信息,请参阅 this post。 - [可选] 只是为了防止副作用,重新安装
Swashbuckle.AspNetCore
包(不需要其他Swashbuckle.AspNetCore.*
包) 确保项目文件中有这两个包(足以使其工作):
PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0"
PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0"
发布(或通过复制文件进行部署)到您的部署文件夹。 现在它应该工作了。
注意:如果您的 API 中有重复的型号名称,有时它会失败(在这种情况下,它会在浏览器中显示一些不清楚的错误);)
尝试删除 .vs
文件夹。在升级到 ASP.NET Core 2.1
我几个小时都遇到了同样的问题,但使用的端口被 Chrome 认为是不安全的(说明这一点的文本是谨慎的,很容易被忽略)。 这可能对将来的某些人有用。
我有同样的问题,我缺少 HttpMethod 绑定。
System.NotSupportedException:操作需要 Swagger 2.0 的显式 HttpMethod 绑定
重新清理并构建您的项目。
此外,请确保控制器中的所有方法都具有 ActionVerbs,即:HttpGet, HttpPost
等等,具有 [ApiExplorerSettings(IgnoreApi = true)]
属性的方法除外。
ActionVerb 和 Route 必须分开:
执行 [HttpGet,Route("getuser")]
而不是 [HttpGet("getuser")]