WebAPI2CLI
Execute ASP.NET Core WebAPI from Command Line . Source at https://github.com/ignatandrei/WebAPI2CLI
Why
What if, instead of running the WebAPI ( or just the site ) and waiting for commands from the user, you want also to execute from the command line some controllers actions ?
This project let's you do that by enabling the command line with
< myexe >.exe --CLI_ENABLED=1 --CLI_Commands=" ... "
The command names are in a cli.txt file that can be generated with
< myexe >.exe --CLI_ENABLED=1 --CLI_HELP=1
How to use ( for .NET Core 3.1 )
Step 0 : install into your ASP.NET Core Web
Install the package https://www.nuget.org/packages/ExtensionNetCore3
Modify your ASP.NET Core as below:
public void ConfigureServices(IServiceCollection services)
{
services.AddCLI();
//your code omitted
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCLI();
//your code omitted
}
And that is all modifications that you need to do for the source code.
Step 1 - find and save the definition of the commands, i.e. WebAPI endpoints
First, you must generate the definition of the commands. For this, we take the OPEN API (swagger ) approach.
For this, after you compile the project, you will run your .exe program with arguments:
< myexe >.exe --CLI_ENABLED=1 --CLI_HELP=1
( or make this from Visual Studio, Project, Properties, Debug )
This will generate a cli.txt file with all definitions of the WebAPI. ( if your API does not appear, check if you have ApiController defined)
Open your cli.txt file and modify the names of the commands as you wish (also , the arguments )
Copy this cli.txt in your solution and be sure that is copied with the exe ( in Visual Studio right click the file, properties, Build Action = Content, CopyToOutputDirectory = Copy if newer)
Step 2 - run the commands
Ensure that the file is near your exe WebAPI.
Run the exe with the following:
< myexe >.exe --CLI_ENABLED=1 --CLI_Commands="your first command,your second command, and enumerate all commands"
The program will run the commands and output the result.
Optional Step 3 - letting others download the app to use as CLI
Modify the endpoints to add zip
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MakeZip(app);
});
and browser to /zip to download the whole application. More details here( including a demo)
https://ignatandrei.github.io/WebAPI2CLI/
Environment Variables: see https://github.com/ignatandrei/Interpreter