Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to generate the documentation of my webservice based on my code comments using swashbuckle. I followed the steps in

ASP.NET 5 MVC 6 API documentation using Swashbuckle Swagger - ASP.NET MVC tutorial for beginners[^]

So I have something like:

private string pathToDoc =
 "C:\\projectname\\artifacts\\bin\\projectname\\Debug\\dnx451\\projectname.xml";
public void ConfigureServices(IServiceCollection services)
{
 // Add framework services.
 services.AddMvc();

 services.AddSwaggerGen();

 services.ConfigureSwaggerDocument(options =>
 {
  options.SingleApiVersion(new Info
  {
   Version = "v1",
   Title = "Geo Search API",
   Description = "A simple api to search using geo location in Elasticsearch",
   TermsOfService = "None"
  });
  options.OperationFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlActionComments(pathToDoc));
 });

 services.ConfigureSwaggerSchema(options =>
 {
  options.DescribeAllEnumsAsStrings = true;
  options.ModelFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlTypeComments(pathToDoc));
 });

 services.AddScoped<ISearchProvider, SearchProvider>();

}


It is working ok, but I don't want to have hardcoded paths. I tried to calculate it using IHostingEnvironment.WebRootPath but it gets the path to a different folder tree.

Is there a way to get this path?
Posted
Updated 14-Jan-16 23:42pm
v2

1 solution

The bigger problem is that this approach will not work at all in Azure, since you won't know the file path in advance.
The solution seems to be to move the XML file to the wwwroot and define the pathToDoc in Startup.cs in the constructor:

C#
public class Startup
    {
        private string pathToDoc;
        public Startup(IHostingEnvironment env)
        {
            // Set up configuration sources.
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .AddEnvironmentVariables();
            Configuration = builder.Build();
            pathToDoc = env.WebRootPath + @"\docweb1.xml";
        }


This is working for me on Azure.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900