XML comments file could not be found - Swagger

前端 未结 8 833
慢半拍i
慢半拍i 2021-01-04 03:10

This is certainly one of those that drives you nuts. As the title indicates all I\'m simply trying to do is display comments pulled from an xml file using swagger.

I

8条回答
  •  耶瑟儿~
    2021-01-04 03:43

    I just wanted to put an answer to this question to further explain why those codes didn't work for Tez (and didn't work for me either).

    The code in question is:

    var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
    

    For this to work, your swagger documentation configuration must be written in the "startup.cs" or in a .cs file within the same project as the controller.

    Also, you must ensure that your XML file is being generated. So, right-click on your project, go to properties, go to build, scroll down to the "output" section, and check the "XML documentation file" option. (These steps are for Visual Studio 2019).

    XML files are usually named "your-project-name".xml by default.

    $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"
    

    Thus, this line of code ensures that the name of the xml file in your swagger configuration will be: "your-project-name".xml, where your controller documentation sits.

    In my case, I had my swagger configuration in an extension method in class library, so that line of code produced the xml file name: "class-library-project-name".xml, when it should have been "controller-resident-project-name".xml.

    The solution, like Tez suggested, is to manually set the XML file name, since you already know what it is.

    Another solution would be to configure swagger in the startup.cs, which is usually in the same project as your controllers.

    Hope this helps.

提交回复
热议问题