gpt4 book ai didi

c# - MVC 核心集 IHostingEnvironment 开发

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:14 25 4
gpt4 key购买 nike

如何手动将 IhostingEnvironment 环境设置为开发环境?我想使用 C# 代码来执行此操作,而不是命令行。

谢谢,

   public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder();

if (env.IsDevelopment())
{

最佳答案

您可以在 Startup 方法中将 env.EnvironmentName 设置为“Development”。

public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder();
env.EnvironmentName = "Development"; // <- Set the EnvironmentName to "Development"

if (env.IsDevelopment())
{

如果您在 here 处看到 IsDevelopment 方法的实现(github 存储库),您会发现它基于 EnvironmentName 的字符串比较操作工作。

    public static bool IsDevelopment(this IHostingEnvironment hostingEnvironment)
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}

return hostingEnvironment.IsEnvironment(EnvironmentName.Development);
}


public static bool IsEnvironment(
this IHostingEnvironment hostingEnvironment,
string environmentName)
{
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}

return string.Equals(
hostingEnvironment.EnvironmentName,
environmentName,
StringComparison.OrdinalIgnoreCase);
}

关于c# - MVC 核心集 IHostingEnvironment 开发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50550537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com