NET5中xml生成的文件的路径

NET5中xml生成的文件的路径

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

问题描述

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



[]



所以我有类似的东西:



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?

推荐答案

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中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 21:54