【问题标题】:Net Core: Difference between .SetBasePath and .UseContentRoot in Startup.CsNet Core:Startup.Cs 中 .SetBasePath 和 .UseContentRoot 的区别
【发布时间】:2019-08-02 07:29:39
【问题描述】:

.SetBasePath 和 .UseContentRoot 在启动配置中有什么区别?

var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();

How can I use IConfiguration from my integration tests?

【问题讨论】:

    标签: c# asp.net-core .net-core


    【解决方案1】:

    SetBasePathIConfigurationBuilder的扩展方法,设置定位配置文件时使用的路径:

    将基于文件的提供程序的 FileProvider 设置为具有基本路径的 PhysicalFileProvider。

    例如,当定位您在问题中指定的appsettings.json 文件时,它将查找使用Directory.GetCurrentDirectory() 检索到的路径

    UseContentRootIWebHostBuilder 的扩展方法,它为Web Host 设置contentRoot 键:

    此设置确定 ASP.NET Core 从何处开始搜索内容文件,例如 MVC 视图。

    contentRoot 键使用的默认值为:

    [...] 应用程序集所在的文件夹。

    这意味着,对于典型设置,两者的路径最终将相同,但这不是必需的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-16
      • 2011-03-13
      • 2013-02-02
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 1970-01-01
      相关资源
      最近更新 更多