简体   繁体   English

Swashbuckle - 从另一个项目的Model中向Swagger UI添加模型和示例值

[英]Swashbuckle - Add Model and Example values to Swagger UI from a Model from another project

I am using Swagger to document my .NET C# API and when my models are on another project Swagger just crashes and doesn't load anything. 我正在使用Swagger来记录我的.NET C# API,当我的模型在另一个项目上时,Swagger只是崩溃而且没有加载任何东西。

When I load the sample WebAPI Project from Visual Studio it uses the models that are on the same project and it works: 当我从Visual Studio加载示例WebAPI项目时,它使用同一项目中的模型并且它可以工作:

Visual Studio示例项目示例图像

But when I use Models from other project it just crashes before loading anything. 但是当我使用其他项目中的模型时,它只会在加载任何内容之前崩溃。

I have an API project and a Business Project. 我有一个API项目和一个商业项目。 My models are View Models stored(and shared among other projects, therfore needed there) on my Buisiness Project. 我的模型是在我的商务项目中存储(并在其他项目之间共享)的视图模型。

Is there any way I can indicate to Swagger where my model definitions are? 有什么方法可以向Swagger表明我的模型定义在哪里?

I have recently handled a similar scenario when I worked on a web API project which was hosted on IIS. 我最近在处理IIS上托管的Web API项目时处理了类似的情况。 I was required to enable xml documentations for all view models and models from two different projects. 我需要为来自两个不同项目的所有视图模型和模型启用xml文档。

Below is a summary of the main steps of the work: 以下是工作主要步骤的摘要:

  1. Enable XML documentation for related projects (refer to here ) 为相关项目启用XML文档(请参阅此处
  2. For each project, build it first and then include the xml file in the project. 对于每个项目,首先构建它,然后在项目中包含xml文件。 Set the property of the file "Copy to Output Directory" to be "Copy is newer" to ensure it is copied to the bin folder of the server. 将文件“复制到输出目录”的属性设置为“复制更新”,以确保将其复制到服务器的bin文件夹。
  3. In the Swagger config, invoke IncludeXmlComments() to include the XML documentation files as suggested by Simon88. 在Swagger配置中,调用IncludeXmlComments()以包含Simon88建议的XML文档文件。

The key point is to ensure all xml documentation files are copied to the bin folder of the host server and Swagger UI knows where they are :-). 关键是确保将所有xml文档文件复制到主机服务器的bin文件夹中,并且Swagger UI知道它们的位置:-)。

By the way, there are few similar questions/answers for this kind of issue. 顺便说一句,这类问题几乎没有类似的问题/答案。 One is here 一个在这里

Hope it helps. 希望能帮助到你。

I faced same issue with Swashbuckle 5.6.0 and managed to fixed as follows. 我遇到了与Swashbuckle 5.6.0相同的问题并设法修复如下。 Enable XML documentation file on model objects containing project by changing project properties as follows. 通过更改项目属性在包含项目的模型对象上启用XML文档文件,如下所示。

在此输入图像描述

Then go to SwaggerConfig.cs file of the api project and add xml comments file of model object project as follows. 然后转到api项目的SwaggerConfig.cs文件并添加模型对象项目的xml注释文件,如下所示。

c.IncludeXmlComments(string.Format(@"{0}\bin\WebApplication1.XML", System.AppDomain.CurrentDomain.BaseDirectory));
// New code line                            
c.IncludeXmlComments(string.Format(@"{0}\bin\core.XML", System.AppDomain.CurrentDomain.BaseDirectory));

Now you will have xml comments for model properties on swagger doc. 现在,您将在swagger doc上获得模型属性的xml注释。

I am actually looking to do the same thing! 我其实想要做同样的事情!

In the Swagger config i tried this, where the functions point to my XML documentation files. 在Swagger配置中,我试过这个,其中的函数指向我的XML文档文件。

c.IncludeXmlComments(GetXmlCommentsPathForControllers());
c.IncludeXmlComments(GetXmlCommentsPathForModels());

It's working fine for the controllers but not much is happening when i document the models. 它对控制器工作正常,但是当我记录模型时并没有发生太多事情。

  1. Source 资源
    1. Models 楷模
    2. Web App 网络应用

If the source of Web application and Model are in different projects, you can always generate the metadata xml and copy them as part of the build into a common location inside the web application. 如果Web应用程序和Model的源代码位于不同的项目中,您始终可以生成元数据xml,并将它们作为构建的一部分复制到Web应用程序内的公共位置。

So in this case the edit the project properties of Models to output xml to "..\\WebApp\\bin" 因此,在这种情况下,编辑Models的项目属性以将xml输出到“.. \\ WebApp \\ bin”

For generating dynamic examples, i would recommend you go through this link . 为了生成动态示例,我建议您浏览此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM