简体   繁体   中英

How to create dotnet core project using c# code?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM