.NET Core 2 和 SwashBuckle Swagger UI 未显示 [英] .NET Core 2 and SwashBuckle Swagger UI is not Displaying
问题描述
我已经学习了一些教程,并且已经让它在工作中工作,但由于某种原因,我无法让 UI 显示,但创建了 Swagger Json.我看的最后一个教程是 这里.
I have followed a few tutorials and have gotten this to work at work but for some reason I am not able to get the UI to display but the Swagger Json is created. Last tutorial I looked at is 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();
});
配置
方法:
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:
Running and visiting the Swagger JSON url produces the appropriate 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 网址时,我得到一个 404.尝试了一些变体.
When visiting the default Swagger UI url I get a 404. Tried a few variations.
- 本地主机:64881/swagger/
- 本地主机:64881/swagger/ui
- localhost:64881/swagger/index.html
- localhost:64881/swagger/ui/index.html
以上所有都返回 404.根据版本,这些都可以正常工作.我错过了什么.
All of the above return 404. These have worked before depending on the versions. What am I missing.
我的完整源代码可以在 GitHub 这里找到.这是这个问题的一个分支,所以代码符合我的要求.
My full source code can be found on GitHub here. This is a branch for this question so the code matches my ask.
推荐答案
对我有什么诀窍:
- 从部署文件夹中删除所有旧文件(我尝试使用
IIS
,也适用于其他托管类型) - 用
Microsoft.AspNetCore.All
元包替换所有Microsoft.AspNetCore.*
包 - 请参阅 这篇文章了解详情. - [可选] 只是副作用,重新安装
Swashbuckle.AspNetCore
包(不需要其他Swashbuckle.AspNetCore.*
包) 确保项目文件中有这两个包(足以让它工作):
- Delete all old files from your deployment folder (I tried with
IIS
, also works for other hosting types) - Replace all
Microsoft.AspNetCore.*
packages withMicrosoft.AspNetCore.All
meta package - see this post for the details. - [optional] Just case of the side effects, reinstall
Swashbuckle.AspNetCore
package (no otherSwashbuckle.AspNetCore.*
packages needed) Make sure you have these 2 packages in the project file (it is enough to make it working):
PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0"
PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0"
发布(或通过复制文件进行部署)到您的部署文件夹.现在它应该可以工作了.
Publish (or deploy by copying the files) to your deployment folder. Now it should work.
注意:如果您的 API 中有重复的模型名称,有时它会失败(在这种情况下,它会在浏览器中显示一些不清楚的错误);)
NOTE: sometimes it fails (in this case it shows some unclear error in the browser) if you have duplicating model names in your API ;)
这篇关于.NET Core 2 和 SwashBuckle Swagger UI 未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!