Home > front end >  How do I use the IncludeXmlComments to point to my second project in the same solution
How do I use the IncludeXmlComments to point to my second project in the same solution

Time:06-20

So I have a solution with two projects, the main project being MultipleProjectsSwaggerDocs and then Public which I added as a reference to the main project. Both of these are ASP.NET Web API (.NET 6) and I have the main project as the startup project.

I'm trying to setup Swagger so that xml comments that are above the function show in the UI, for instance. enter image description here

I've actually managed to get it to work by following the guide over enter image description here

But as you can see, the path to the other xml file is hardcoded, and that's not good if I were to host the project, because it wouldn't find the file. Is there something I can do in order to point it to the second project?

And if this is not the correct way to setup AddSwaggerGen with two projects in one solution, then what is?

CodePudding user response:

See, it is not necessary to hardcode the full path to the second xml comments file. When you connected the first file, you wrote Path.Combine(AppContext.BaseDirectory, xmlFilename). Nobody forbids you to do the same with the second :)

For example, I usually use the following code to connect my xml comments to Swagger:

string[] allMyXmlCommentFileNames =
{
    $"{Assembly.GetExecutingAssembly().GetName().Name}.xml",
    "Public.xml"
};
foreach (string fileName in allMyXmlCommentFileNames)
{
    string xmlFilePath = Path.Combine(AppContext.BaseDirectory, fileName);
    if (File.Exists(xmlFilePath))
        options.IncludeXmlComments(xmlFilePath, includeControllerXmlComments: true);
}

Some theory why this should work

For example, if you open the Public\Public.csproj file, then in my case Rider will generate the following code for me in this file if I add xml comment generation to this project:

<DocumentationFile>bin\Debug\net6.0\Public.xml</DocumentationFile>

And when you build a .NET project, all projects linked to it are added as separate dlls to the bin\Debug\net6.0 folder of the project you are building. And it works for xml comments too, they will be there too.

  • Related