[英]swagger.json paths and definitions are empty. No operations defined in spec
I am developing a .netcore web application.我正在开发一个 .netcore Web 应用程序。 I am using of swagger and I've made all the necessary adjustments.
我正在使用 swagger 并进行了所有必要的调整。 Unfortunately it does not work and I just see
No operations defined in spec!
不幸的是它不起作用,我只看到
No operations defined in spec!
in the swagger output page.在 swagger 输出页面中。
The swagger file with /swagger/v1/swagger.json
has the following content:带有
/swagger/v1/swagger.json
的 swagger 文件具有以下内容:
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "Something"
},
"paths": {},
"definitions": {}
}
I want to see my controllers and their actions in swagger output page.我想在 swagger 输出页面中查看我的控制器及其操作。
after some research i was found that my problem was about using swagger along with OData in .NetCore2.1. 经过一番研究,我发现我的问题是关于在.NetCore2.1中将swagger与OData一起使用。 i found a solution for this problem.
我找到了解决这个问题的方法。
first i added two following Nuget packages: 首先,我添加了以下两个Nuget软件包:
Swashbuckle.AspNetCore
Swashbuckle.AspNetCore.Annotations
then, i added following codes in Startup.cs 然后,我在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);
the, i added following code line in controllers: 的,我在控制器中添加了以下代码行:
[ApiExplorerSettings(IgnoreApi = false)]
please note that it was worked for me but it may be need more research for eventually side effects 请注意,它对我有用,但可能需要更多研究以最终发现副作用
Please add a in contoller any method like a then showing swagger methods请在控制器中添加任何方法,例如然后显示 swagger 方法
[HttpGet]
[EnableQuery]
public IQueryable<int> Get()
{
return 1;
}
Sounds like you need to add some routes in the controller.听起来您需要在控制器中添加一些路由。 With rest services you will need to add them separate from what was built automatically by Visual Studio.
对于休息服务,您需要将它们与 Visual Studio 自动构建的内容分开添加。 for example // GET: Items [HttpGet] [Route("/items")] ...rest of function... this will give your swagger a reference to what it does when you click the button.
例如 // GET: Items [HttpGet] [Route("/items")] ...其余的功能...这会给你的招摇参考当你点击按钮时它会做什么。
you need to enable XML Documentation file
under project obtions => Build
tab. 您需要在
project obtions => Build
选项卡下启用XML Documentation file
。
Then you need to read this file through swagger so that swagger can create documentation from it. 然后,您需要通过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};
}
}
In ConfigureServices 在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.