[英]How do you pass a variable from Program.CS to a Controller in .NET Core 2.1?
I will apologize in advance that I am a complete novice with .NET so please bear with me.我会提前道歉,我是 .NET 的新手,所以请多多包涵。 My goal is to simply be able to pass variables from my Program class to a controller in my setup.
我的目标是简单地将变量从我的程序 class 传递到我的设置中的 controller。
Program.cs程序.cs
using System;
using System.Collections.Generic;
using System.IO;
namespace EJ2FileManagerService
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Run();
}
public static IWebHost CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var test = "results";
})
.UseStartup<Startup>()
.Build();
}
}
I want to be able to consume the variable test
in my TestController
class.我希望能够在我的
TestController
class 中使用变量test
。 It is located under Controllers/TestController.cs.它位于 Controllers/TestController.cs 下。
TestController.cs测试控制器.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
namespace EJ2FileManagerServices.Controllers
{
[Route("api/[controller]")]
public class TestController : Controller
{
// WANT TO READ TEST VARIABLE HERE
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
}
}
This was simply just user error -- the first comment was correct I just needed to do my calls and actions inside the controller versus inside of Program.cs.这只是用户错误——第一条评论是正确的,我只需要在 controller 内部而不是 Program.cs 内部进行调用和操作。
Thanks!谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.