Home > Back-end >  Passing an argument from .NET MVC APP into .NET Console APP
Passing an argument from .NET MVC APP into .NET Console APP

Time:05-20

I have an MVC app in which I have integrated a .net console app. Now I want to pass a value that is returned from a method in MVC Model and pass it as an argument in the console app MAIN method.

I want that the "hexString" that is being returned from "PinBlock" function is passed as an argument in the Main Method in the console application.

**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();

           
            }
        

CodePudding user response:

In the console app we just need to output the first argument as:

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();
  • Related