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?

7
  • 1
    You can call dotnet cli for that, if sdk is installed on machine, where your web app is running Commented Apr 2, 2020 at 12:23
  • How can I call cli from application? Commented Apr 2, 2020 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# Commented Apr 2, 2020 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. Commented Apr 2, 2020 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. Commented Apr 2, 2020 at 14:20

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It'll throw 'Access denied'
You new admin privilege to start external process.
How do you publish your backend to execute admin tasks? I think it's not in the nature of it all, plus, I assume, it could start a process, but only if it's in the local path and not outside, and doesn't require admin privileges.
Well there are several ways to handle permission which has nothing to do with your actual question. Of course you should consider of security matter in this case.
Then, please, handle permissions in your code and post it in its working version.

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.