gpt4 book ai didi

swagger - .NET Core 2 和 SwashBuckle Swagger UI 未显示

转载 作者:行者123 更新时间:2023-12-04 04:58:46 25 4
gpt4 key购买 nike

我已经遵循了一些教程,并且已经在工作中使用了它,但是由于某种原因,我无法显示 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。尝试了一些变体。
  • 本地主机:64881/swagger/
  • 本地主机:64881/swagger/ui
  • 本地主机:64881/swagger/index.html
  • 本地主机:64881/swagger/ui/index.html

  • 以上所有都返回 404。这些之前都有效,具体取决于版本。我错过了什么。

    我的完整源代码可以在 GitHub 上找到 here .这是这个问题的一个分支,所以代码符合我的要求。

    最佳答案

    对我有什么好处:

  • 从部署文件夹中删除所有旧文件 (我尝试过 IIS ,也适用于其他托管类型)
  • 全部替换 Microsoft.AspNetCore.*包裹 Microsoft.AspNetCore.All元包 - 见 this post详情。
  • [可选] 以防万一,重新安装 Swashbuckle.AspNetCore包(不需要其他 Swashbuckle.AspNetCore.* 包)
  • 确保项目文件中有这 2 个包(足以使其工作):
  • PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0"
  • PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0"
  • 发布(或通过复制文件进行部署)到您的部署文件夹。
    现在它应该可以工作了。

  • 注意:如果您的 API 中有重复的模型名称,有时它会失败(在这种情况下,它会在浏览器中显示一些不清楚的错误);)

    关于swagger - .NET Core 2 和 SwashBuckle Swagger UI 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45850636/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com