如何在 Program cs 中声明全局变量并在 .NET 6.0 Web 的控制器中使用它 Api
How to declare global variable in Program cs and use it in controllers in .NET 6.0 Web Api
我在 .NET 6.0 中有来自 Web Api 模板的默认 Program.cs 文件。
我正在添加变量“test”,以便我可以在控制器中使用它的值。
var builder = WebApplication.CreateBuilder(args);
const string test = "test123";
builder.Configuration.Bind(test);
//rest of the file...
现在我想在 Program.cs 之外使用变量“test”,但我不知道该怎么做。我不能简单地使用它,因为当尝试在控制器中读取它时:
string localVar = test;
我收到错误消息“'test' 此处不为空。无法在此上下文中使用在顶级语句中声明的局部变量或局部函数”。
这可能是一些愚蠢的错误,但我想不通...
从 C# 9 开始,我们不需要在 Program.cs
文件中明确提及 Main
方法,因为我们可以使用 top-level 语句功能。但是,这并不意味着我们根本不应该在创建的文件中使用默认程序 class。在您的情况下,您需要定义 static/const 属性 以便将新创建的结构更改为旧结构。
namespace WebApplication;
public class Program
{
public static string Test { get; private set; }
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
Program.Test = "approach1";
builder.Services.Configure<MyOptions>(x => x.Test = "approach2");
///
}
public class MyOptions
{
public string Test { get; set; }
}
我假设您需要在运行时将值设置为 Program.Test
字段,因此在第一种方法中,我使用 static
字段和 private set;
访问器而不是常量。
在第二种方法中,我使用了C#选项功能来配置MyOptions.Test
字段值,这将非常灵活并且对以后编写单元测试很有用。但是,您需要在需要的地方注入 MyOptions
class。
在下面的控制器模板中,我在 Get
方法
中指定了如何访问 Program.cs
文件中的配置值
public class TestController : ControllerBase
{
private readonly MyOptions _myOptions;
public TestController (IOptions<MyOptions> myOptions)
{
_myOptions = myOptions.Value;
}
public IActionResult Get()
{
string test1 = Program.Test;
string test2 = _myOptions.Test;
///
}
}
在 Program.cs 文件的末尾添加 public partial class Program { }
并添加常量,属性 或您喜欢的任何内容。
我在 .NET 6.0 中有来自 Web Api 模板的默认 Program.cs 文件。 我正在添加变量“test”,以便我可以在控制器中使用它的值。
var builder = WebApplication.CreateBuilder(args);
const string test = "test123";
builder.Configuration.Bind(test);
//rest of the file...
现在我想在 Program.cs 之外使用变量“test”,但我不知道该怎么做。我不能简单地使用它,因为当尝试在控制器中读取它时:
string localVar = test;
我收到错误消息“'test' 此处不为空。无法在此上下文中使用在顶级语句中声明的局部变量或局部函数”。 这可能是一些愚蠢的错误,但我想不通...
从 C# 9 开始,我们不需要在 Program.cs
文件中明确提及 Main
方法,因为我们可以使用 top-level 语句功能。但是,这并不意味着我们根本不应该在创建的文件中使用默认程序 class。在您的情况下,您需要定义 static/const 属性 以便将新创建的结构更改为旧结构。
namespace WebApplication;
public class Program
{
public static string Test { get; private set; }
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
Program.Test = "approach1";
builder.Services.Configure<MyOptions>(x => x.Test = "approach2");
///
}
public class MyOptions
{
public string Test { get; set; }
}
我假设您需要在运行时将值设置为 Program.Test
字段,因此在第一种方法中,我使用 static
字段和 private set;
访问器而不是常量。
在第二种方法中,我使用了C#选项功能来配置MyOptions.Test
字段值,这将非常灵活并且对以后编写单元测试很有用。但是,您需要在需要的地方注入 MyOptions
class。
在下面的控制器模板中,我在 Get
方法
Program.cs
文件中的配置值
public class TestController : ControllerBase
{
private readonly MyOptions _myOptions;
public TestController (IOptions<MyOptions> myOptions)
{
_myOptions = myOptions.Value;
}
public IActionResult Get()
{
string test1 = Program.Test;
string test2 = _myOptions.Test;
///
}
}
在 Program.cs 文件的末尾添加 public partial class Program { }
并添加常量,属性 或您喜欢的任何内容。