简体   繁体   English

如何在ASP.NET Core MVC应用程序中访问命令行参数?

[英]How do I access command line parameters in an ASP.NET Core MVC app?

My goal is to use arguments from the command line (namely, a username + token) later in one of the controllers. 我的目标是稍后在其中一个控制器中使用命令行中的参数(即用户名+令牌)。

  • This blog explained a lot of useful setup, but not how to actually use custom arguments other than --environment . 这个博客解释了很多有用的设置,但不是如何实际使用除--environment之外的自定义参数。
  • I've found ways to pass command line arguments into the Startup class , but not how to get it into the controller. 我已经找到了将命令行参数传递到Startup类的方法 ,但没有找到如何将它传入控制器的方法。
  • Upon looking at the docs , I cannot correctly pass the parameters to controller classes (the sample file also conflates the Program and Startup classes, so I am not sure how to handle that in my own code). 在查看文档时 ,我无法正确地将参数传递给控制器​​类(示例文件也会混淆Program和Startup类,所以我不知道如何在我自己的代码中处理它)。

Ultimately I need to have some value configured at the beginning of the app, and this value needs to be accessed by controller methods later. 最终,我需要在应用程序的开头配置一些值,稍后需要通过控制器方法访问此值。 Any alternatives that achieve this are also greatly appreciated. 任何实现这一目标的替代方案也非常受欢迎。

ASP.NET Core is shipped with built-in DI container , that should be used for dependency resolving via constructor parameters. ASP.NET Core附带内置的DI容器 ,应该用于通过构造函数参数进行依赖性解析。

I've found ways to pass command line arguments into the Startup class , but not how to get it into the controller. 我已经找到了将命令行参数传递到Startup类的方法 ,但没有找到如何将它传入控制器的方法。

This example already has everything that you need. 这个例子已经包含了你需要的一切。 See: 看到:

.ConfigureServices(services => services
                .AddSingleton(new ConsoleArgs(args))

this line register ConsoleArgs instance as a Singleton to DI container. 此行将ConsoleArgs实例注册为Singleton到DI容器。 Then it could be used as the dependency in any class. 然后它可以用作任何类中的依赖项。 Like into Startup class in linked example: 就像链接示例中的Startup类一样:

// args will be resolved  using DI container
public Startup(IHostingEnvironment env, ConsoleArgs args)
{      
  ...

So in case of your controller class do the same: 因此,如果你的控制器类做同样的事情:

public class YourController : Controller
{
    public YourController(ConsoleArgs args)
    {
        //use args here or store it in private variable
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用命令行如何在 ASP.NET 中首先从 DB 进行脚手架 - 而不是 ASP.NET Core MVC? - Using command line how to do scaffolding from DB first in ASP.NET - not ASP.NET Core MVC? 如何在 ASP.NET Core MVC“待办事项”应用程序中提取 URL 的文件名并在视图中显示给用户? - How do I extract the filename of a URL in a ASP.NET Core MVC "To Do" app and show it to the user in the View? 如何向我的 MVC asp.net Core 应用程序添加排序功能? - How do I add a sorting feature to my MVC asp.net Core app? 我如何与asp.net的命令行应用程序进行交互 - How do I interact with a command line app from asp.net 如何在 docker-compose 命令中将 appsettings.json 值替换为 .env 变量以启动 ASP.NET 核心 MVC 应用程序? - How do I replace appsettings.json values with .env variable in docker-compose command to launch an ASP.NET Core MVC app? 如何在 ASP.NET Core MVC 中使用 ADO.NET 将参数添加到存储过程? - How can I add parameters to a stored procedure using ADO.NET in ASP.NET Core MVC? 如何将两个参数传递给 ASP.NET Core MVC 中的 controller? - How to pass two parameters to a controller in ASP.NET Core MVC? 在控制台应用程序中重用来自我的 ASP.NET Core MVC 应用程序的代码:如何进行数据库连接 - Reusing code from my ASP.NET Core MVC app in console app : how to do database connection 如何使用 ASP.NET Core 3.1 中的参数进行路由 - How to do routing with parameters in ASP.NET Core 3.1 如何解决 ASP.NET Core MVC 应用程序中的迁移问题? - How to solve migration problem in ASP.NET Core MVC app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM