使用 .NET Core 传递多个参数时,Node 命令未返回输出

问题描述 投票:0回答:1

我正在执行一个节点命令,该命令在 .NET Core 8 环境中接受两个以上参数。但我得到一个空白的输出。

下面是我在 PowerShell 窗口中使用的节点命令并获取输出:

node rsa-sign.js ./privatekey.pem <Unique identity number>

在上面的命令中,节点运行此

rsa-sign.js
文件,并从具有唯一标识号的
.pem
文件中获取私钥,并在 PowerShell 窗口中生成一些签名。

我也在 .NET Core 中尝试实现同样的目标。

为了实现这一点,这是我迄今为止所尝试的:

[HttpPost]
public IActionResult GenerateSignature(string fPath, string identity)
{
    string _rsaFilePath = Path.Combine(_webHostEnvironment.WebRootPath + @"\js\", "rsa-sign.js");
    string nodeCommand = _rsaFilePath; // Node.js script name or path
    string privateKeyPath = fPath;
    string inputValue = identity;

    string output = ExecuteNodeCommand(nodeCommand, $"{privateKeyPath} {inputValue}");

    return Json(output);
}

private string ExecuteNodeCommand(string scriptName, string arguments)
{
    try
    {
        //string scriptOutput = ExecuteNodeCommand(nodeCommand, $"{privateKeyPath} {inputValue}");
    
        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            FileName = "node",
            Arguments = $"{scriptName} {arguments}",
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        using (Process process = new Process())
        {
            process.StartInfo = startInfo;
            process.Start();

            string output = process.StandardOutput.ReadToEnd();

            process.WaitForExit();

            return output.Trim();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error executing Node.js command: {ex.Message}");
        return string.Empty;
    }
}

但是当我检查输出变量时,它只是空白。

我需要你的帮助。

c# node.js asp.net-core-mvc
1个回答
0
投票
private string ExecuteNodeCommand(string scriptName, string privateKeyPath, string identity)
{
    try
    {
        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            FileName = "node",
            Arguments = $"{scriptName} {privateKeyPath} {identity}", // Pass arguments separately
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        using (Process process = new Process())
        {
            process.StartInfo = startInfo;
            process.Start();

            string output = process.StandardOutput.ReadToEnd();

            process.WaitForExit();

            return output.Trim();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error executing Node.js command: {ex.Message}");
        return string.Empty;
    }
}

[HttpPost]
public IActionResult GenerateSignature(string fPath, string identity)
{
    string _rsaFilePath = Path.Combine(_webHostEnvironment.WebRootPath + @"\js\", "rsa-sign.js");
    string privateKeyPath = fPath;
    string inputValue = identity;

    string output = ExecuteNodeCommand(_rsaFilePath, privateKeyPath, inputValue);

    return Json(output);
}
© www.soinside.com 2019 - 2024. All rights reserved.