0

We can create solutions and project using dotnet core command line. But I want to run this commands in my web application and create a project automatically and download it.

[HttpPost("create")]
public async Task<IActionResult> Create([FromBody] string projectName)
{
    // dotnet dotnet new sln --name projectName

   return created solution zip file
}

Is this possible?

barteloma
  • 6,403
  • 14
  • 79
  • 173
  • 1
    You can call dotnet cli for that, if sdk is installed on machine, where your web app is running – Pavel Anikhouski Apr 02 '20 at 12:23
  • How can I call cli from application? – barteloma Apr 02 '20 at 12:31
  • in the same way with any command line, there are a lot of questions about it, like this [Run command line code programmatically using C#](https://stackoverflow.com/questions/13738168/run-command-line-code-programmatically-using-c-sharp) – Pavel Anikhouski Apr 02 '20 at 12:34
  • Servers' security policies and restrictions won't let you run 'dotnet' process directly. I see only one solution. You can create templates on side, then you could just rename things in your code, package them and send them back to users. – Arthur Grigoryan Apr 02 '20 at 12:59
  • Create all projects templates and save them as Zip file, then return it as file response. easy solution without a security issue. – XAMT Apr 02 '20 at 14:20
  • @XAMT this is another way for solution, but how can I set name of peoject solution? – barteloma Apr 02 '20 at 15:42
  • @barteloma; Save all files in MS SQL file table, Rename the csproj file in runtime and return files as zip file. Or using Install package products (e.g. InstallShield). – XAMT Apr 02 '20 at 15:52

1 Answers1

0

I haven't test this code but it should be something like this

[HttpGet("create-proj")]
public ActionResult CreatProject(string name)
{
    var args = "dotnet dotnet new sln --name {name}";

    var p = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe",
            Arguments = args,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true,
            WorkingDirectory = Directory.GetCurrentDirectory(),
        }
    };

    p.Start();
    p.WaitForExit();

   // Zip solution in WorkingDirectory and return that file
}

Make sure you installed .NET Core SDK on machine.

Mohsen Esmailpour
  • 11,224
  • 3
  • 45
  • 66