Skip to content

Show data that is obtained from command line process in ASP.NET MVC

In c# wpf, I can add command-line-based apps to my app and send input and show the output in a richtextbox using System.Diagnostics. Now I wanna implement this in ASP.NET MVC.

I know how to send data from javascript to a controller using ajax and the controller does some processing and returns a result and shows it in view. but the thing is, the data from that process is receiving asynchronously by an event like this:

private void SortOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
  // For each line that is printed in the console, this event will be executed and the 
  // text from that line can be received by outLine.data
}

What I’m trying to do is add this received text (outline.data) to a paragraph in view asynchronously. Ajax only works if I wanted to run an ActionResult method and wait for my response and after all of the lines are received, then it will send the result back to javascript. But I want to show every received line exactly the moment it arrived in view. How can I do it?

Answer

Using SignalR solved my issue. With that library, I could call an asp.net function and send data in javascript and vice versa. to recognize every client, I stored their connection id to send the result back to them when it’s ready.