将参数从 .NET MVC APP 传递到 .NET Console APP

Passing an argument from .NET MVC APP into .NET Console APP

我有一个 MVC 应用程序,我在其中集成了一个 .net 控制台应用程序。现在我想传递一个从 MVC 模型中的方法返回的值,并将其作为参数传递到控制台应用程序的 MAIN 方法中。

我希望将从“PinBlock”函数返回的“hexString”作为参数传递到控制台应用程序的 Main 方法中。

**MVC Model Function**

public string PinBlock()
        {
            // PAN Code without first 3 characters and last character
            string str1 = Convert.ToString(PAN).Remove(0, 3);
            string str2 = str1.Remove(str1.Length - 1, 1);

            // Adding 4 0s at the start of the remaining PAN Number 
            int count = 4;
            char someChar = '0';
            string AlgoA = str2.PadLeft(count + str2.Length, someChar);
            Console.WriteLine("ALgoA: " + AlgoA);

            //Finding the length of the pin code and adding it to the pin code wth 10 'F's
            string PinLength = Convert.ToString(APIN);
            PinLength = PinLength.ToString();
            string result = String.Format("{0,2:X2}", PinLength.Length);
            string AlgoB = result + APIN + "FFFFFFFFFF";
            Console.WriteLine("ALgoB: " + AlgoB);

            long dec1 = Convert.ToInt64(AlgoA, 16);
            long dec2 = Convert.ToInt64(AlgoB, 16);
            long res = dec1 ^ dec2;
            string hexResult = res.ToString("X");
            PIN = hexResult;
            return hexResult;

        }


**MVC Function that attaches Console app with the MVC app**

public string Onclick(string hexResult)
        {

            using (var process = new Process())
            {
                process.StartInfo.FileName = @"..\ABL-ESB-DCA\bin\Debug\net6.0\encrypt\ConsoleApp1.exe"; // relative path. absolute path works too.
                process.StartInfo.Arguments = hexResult;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;

                process.OutputDataReceived += (sender, data) =>
                {
                    Console.WriteLine(data.Data);





                };
                process.ErrorDataReceived += (sender, data) => Console.WriteLine(data.Data);
                Console.WriteLine("starting");
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                var exited = process.WaitForExit(1000 * 10);     // (optional) wait up to 10 seconds
                Console.WriteLine($"exit {exited}");
            }



            // ProcessInfo.Start(@"C:\Users\Talha\Desktop\New folder\linkedin\server.exe");
            return "BUTTON ONCLICK";
        }

**Console app**

internal class Program
    {
        static void Main(string[] args)
        {

            ClassLibrary1.Class1 class1 = new ClassLibrary1.Class1(); ;
            class1.Perform();

           
            }
        

在控制台应用程序中,我们只需要将第一个参数输出为:

Console.WriteLine(arg[0]);

static void Main(string[] args)
        {
            foreach(string arg in args)
            {

                Console.WriteLine(arg[0]);
            
            }

            ClassLibrary1.Class1 class1 = new ClassLibrary1.Class1(); ;
            class1.Perform();