0

I'm creating a WebApi in .net core 5, and I wanted it to call some CLI commands to create projects inside a solution that will be dynamically created. And if possible, I would like to receive the return of CLI commands within .net. Is it possible to do this?

I will have a javascript and HTML interface that will call this API (to create projects dynamically) and show the result of the creation (CLI return)

enter image description here

1
  • I searched a lot of information and found no relevant information. I am afraid it is difficult to implement in .net core WebAPI Commented Aug 25, 2021 at 9:34

1 Answer 1

1

I think this example will be useful

    [HttpGet("{name}")]
    public async Task<IActionResult> Create(string name)
    {
        string outputText = string.Empty;
        var standardError = string.Empty;

        try
        {
            using (Process process = new Process())
            {
                process.StartInfo = new ProcessStartInfo
                {
                    FileName = "dotnet",
                    Arguments = @$"new sln -o {name}",
                    WorkingDirectory = @"C:\projects",//the file must exist
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    UseShellExecute = false,
                    CreateNoWindow = false,
                };

                process.Start();
                outputText = process.StandardOutput.ReadToEnd();
                outputText = outputText.Replace(Environment.NewLine, string.Empty);
                standardError = process.StandardError.ReadToEnd();
                process.WaitForExit();
            }
        }
        catch (Exception ex)
        {

        }
        return Ok(outputText + standardError);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.