![](/img/trans.png)
[英]How to order/sort paths in NSwag swagger.json by controller name
[英]swagger.json paths and definitions are empty. No operations defined in spec
我正在開發一個 .netcore Web 應用程序。 我正在使用 swagger 並進行了所有必要的調整。 不幸的是它不起作用,我只看到No operations defined in spec!
在 swagger 輸出頁面中。
帶有/swagger/v1/swagger.json
的 swagger 文件具有以下內容:
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "Something"
},
"paths": {},
"definitions": {}
}
我想在 swagger 輸出頁面中查看我的控制器及其操作。
經過一番研究,我發現我的問題是關於在.NetCore2.1中將swagger與OData一起使用。 我找到了解決這個問題的方法。
首先,我添加了以下兩個Nuget軟件包:
Swashbuckle.AspNetCore
Swashbuckle.AspNetCore.Annotations
然后,我在Startup.cs中添加了以下代碼
services.AddMvc(options => {
foreach (var outputFormatter in
options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ =>
_.SupportedMediaTypes.Count == 0))
{
outputFormatter.SupportedMediaTypes.Add(new
MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}
foreach (var inputFormatter in
options.InputFormatters.OfType<ODataInputFormatter>().Where(_ =>
_.SupportedMediaTypes.Count == 0))
{
inputFormatter.SupportedMediaTypes.Add(new
MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
的,我在控制器中添加了以下代碼行:
[ApiExplorerSettings(IgnoreApi = false)]
請注意,它對我有用,但可能需要更多研究以最終發現副作用
請在控制器中添加任何方法,例如然后顯示 swagger 方法
[HttpGet]
[EnableQuery]
public IQueryable<int> Get()
{
return 1;
}
聽起來您需要在控制器中添加一些路由。 對於休息服務,您需要將它們與 Visual Studio 自動構建的內容分開添加。 例如 // GET: Items [HttpGet] [Route("/items")] ...其余的功能...這會給你的招搖參考當你點擊按鈕時它會做什么。
您需要在project obtions => Build
選項卡下啟用XML Documentation file
。
然后,您需要通過swagger讀取此文件,以便swagger可以從中創建文檔。
private static string[] XmlCommentsFilePath
{
get
{
var basePath = PlatformServices.Default.Application.ApplicationBasePath;
var apiDocFile = typeof(Startup).GetTypeInfo().Assembly.GetName().Name + ".xml";
var apiPath = Path.Combine(basePath, apiDocFile);
return new[] {apiPath};
}
}
在ConfigureServices中
services.AddSwaggerGen(options =>
{
var provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>();
// add a swagger document for each discovered API version
provider.ApiVersionDescriptions.ForEach(x => options.SwaggerDoc(x.GroupName, CreateInfoForApiVersion(x)));
....
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.