获取ASP.NET5中xml生成的文件的路径,以便在Swashbuckle中使用 [英] Get path of xml generated files in ASP.NET5 for using in Swashbuckle

查看:210
本文介绍了获取ASP.NET5中xml生成的文件的路径,以便在Swashbuckle中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用swashbuckle根据我的代码注释生成我的webservice的文档。我按照



使用Swashbuckle Swagger的ASP.NET 5 MVC 6 API文档 - 面向初学者的ASP.NET MVC教程 [ ^ ]



所以我有类似的东西:



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>();

}





工作正常,但我不想拥有硬编码路径。我尝试使用IHostingEnvironment.WebRootPath计算它,但它获取到不同文件夹树的路径。



有没有办法获得这条路?



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?

推荐答案

更大的问题是这种方法会在Azure中根本不工作,因为你事先不知道文件路径。

解决方案似乎是将XML文件移动到wwwroot并在Startup.cs中定义pathToDoc构造函数:



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:

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.


这篇关于获取ASP.NET5中xml生成的文件的路径,以便在Swashbuckle中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆