简体   繁体   English

如何将 Program.CS 中的变量传递给 .NET Core 2.1 中的 Controller?

[英]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.

相关问题 如何在不使用启动 class 的情况下将其迁移到我的 .NET 6 Program.cs 文件中? 代码来自 .NET Core 2.1,也在 Program.cs - How do I migrate this into my .NET 6 Program.cs file without using the startup class? The code is from .NET Core 2.1, also in Program.cs .NET核心从Program.cs传递命令行Args到Startup.cs - .NET core Pass Commandline Args to Startup.cs from Program.cs 从 .NET Core Program.cs 访问配置 - Access Configuration from .NET Core Program.cs 如何在 ASP.NET CORE 2.1 中的 Program.cs 文件中检查代码是在开发模式还是生产模式下运行? - How to check whether the code is running in development or Production mode in Program.cs file in ASP.NET CORE 2.1? 如何返回在 Program.cs ASP.Net core 6 中找不到 - How to return not found in Program.cs ASP.Net core 6 如何从 program.cs 中的 appsettings 中读取 UrlPrefixes - asp.net core 3.1 - how to read UrlPrefixes from appsettings in program.cs - asp.net core 3.1 如何从 ASP.NET Core 中的 Program.cs 访问 IWebHostEnvironment - How to access IWebHostEnvironment from Program.cs in ASP.NET Core .Net5:如何从 Program.cs 获取 EnvironmentName - .Net5: How do I get the EnvironmentName from Program.cs .NET Core program.cs 和 Startup.cs 未命中 - .NET Core program.cs and Startup.cs not hit 如何在 .NET 6 Program.cs 中创建记录器 - How to create a Logger in .NET 6 Program.cs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM