c# - Net Core : Difference between . Startup.Cs 中的 SetBasePath 和 .UseContentRoot

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

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

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

How can I use IConfiguration from my integration tests?

最佳答案

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

Sets the FileProvider for file-based providers to a PhysicalFileProvider with the base path.



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

UseContentRoot IWebHostBuilder 的扩展方法, 设置 contentRoot Web 主机的 key :

This setting determines where ASP.NET Core begins searching for content files, such as MVC views.


contentRoot 使用的默认值关键是:

[...] the folder where the app assembly resides.



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

关于c# - Net Core : Difference between . Startup.Cs 中的 SetBasePath 和 .UseContentRoot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57321773/

相关文章:

c# - 时间跨度比较

c# - 添加 Claims Transformer 后注入(inject)的 IPrincipal Claims 为空

c# - 如何在 asp net core 3.1 中保持托管服务的事件状态?

azure - 在 Azure 门户中哪里可以找到自定义指标

c# - 使用 Roslyn CSharpCompilation 生成 .NET Standard 2.0 DLL

c# - WPF Datagrid 列宽最大化时调整大小

c# - 如何在 WPF 中使用 GDI+?

c# - 当 header 包含非 ascii 内容时,System.Net.Browser.ClientHttpWebRequest.EndGetResponse 抛出 System.ArgumentException

c# - EF 核心 : drop and recreate database when server restarts

asp.net-core - 自定义错误页面 .Net Core Web 应用程序未显示 500 错误