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项目时,它使用同一项目上的模型,它可以工作:
But when I use Models from other project it just crashes before loading anything.
但是当我使用其他项目中的模型时,它只会在加载任何内容之前崩溃。
I have an API project and a Business Project. My models are View Models stored(and shared among other projects, therfore needed there) on my Buisiness Project.
我有一个API项目和一个商业项目。我的模型是在我的商务项目中存储(并在其他项目之间共享)的视图模型。
Is there any way I can indicate to Swagger where my model definitions are?
有什么方法可以向Swagger表明我的模型定义在哪里?
4 个解决方案
#1
0
I have recently handled a similar scenario when I worked on a web API project which was hosted on IIS. I was required to enable xml documentations for all view models and models from two different projects.
我最近在处理IIS上托管的Web API项目时处理了类似的情况。我被要求为两个不同项目的所有视图模型和模型启用xml文档。
Below is a summary of the main steps of the work:
以下是工作主要步骤的摘要:
- Enable XML documentation for related projects (refer to here)
- For each project, build it first and then include the xml file in the project. 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.
- In the Swagger config, invoke IncludeXmlComments() to include the XML documentation files as suggested by Simon88.
为相关项目启用XML文档(请参阅此处)
对于每个项目,首先构建它,然后在项目中包含xml文件。将“Copy to Output Directory”文件的属性设置为“Copy is newer”以确保将其复制到服务器的bin文件夹中。
在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.
希望能帮助到你。
#2
0
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.
它对控制器工作正常,但是当我记录模型时并没有发生太多事情。
#3
0
- Source
- Models
- Web App
Source Models 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.
为了生成动态示例,我建议您浏览此链接。
#4
0
I faced same issue with Swashbuckle 5.6.0 and managed to fixed as follows. Enable XML documentation file on model objects containing project by changing project properties as follows.
我遇到了与Swashbuckle 5.6.0相同的问题,并设法修复如下。通过更改项目属性在包含项目的模型对象上启用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注释。
#1
0
I have recently handled a similar scenario when I worked on a web API project which was hosted on IIS. I was required to enable xml documentations for all view models and models from two different projects.
我最近在处理IIS上托管的Web API项目时处理了类似的情况。我被要求为两个不同项目的所有视图模型和模型启用xml文档。
Below is a summary of the main steps of the work:
以下是工作主要步骤的摘要:
- Enable XML documentation for related projects (refer to here)
- For each project, build it first and then include the xml file in the project. 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.
- In the Swagger config, invoke IncludeXmlComments() to include the XML documentation files as suggested by Simon88.
为相关项目启用XML文档(请参阅此处)
对于每个项目,首先构建它,然后在项目中包含xml文件。将“Copy to Output Directory”文件的属性设置为“Copy is newer”以确保将其复制到服务器的bin文件夹中。
在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.
希望能帮助到你。
#2
0
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.
它对控制器工作正常,但是当我记录模型时并没有发生太多事情。
#3
0
- Source
- Models
- Web App
Source Models 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.
为了生成动态示例,我建议您浏览此链接。
#4
0
I faced same issue with Swashbuckle 5.6.0 and managed to fixed as follows. Enable XML documentation file on model objects containing project by changing project properties as follows.
我遇到了与Swashbuckle 5.6.0相同的问题,并设法修复如下。通过更改项目属性在包含项目的模型对象上启用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注释。