简体   繁体   中英

Wait for Controller Action to be Called in MVC Method

I'm trying to execute a remote process using C# in ASP.Net MVC which will POST data back to one of my controllers. Is there a way to wrap this controller action in an event within my calling method so I can wait for the POST data? I am not sure what options I have, if any. I am hoping for something like:

Calling Method:

public void MainMethod()
{ 
    ExecuteRemoteProcess();
    //Listen for PostData();
}

Controller:

[HttpPost]
public string PostData()
{
    var length = Request.ContentLength;
    Stream req = Request.InputStream;
    req.Seek(0, System.IO.SeekOrigin.Begin);
    string json = new StreamReader(req).ReadToEnd();
    return json;
}

Edit: MainMethod is a separate class method within the MVC app that would simply call an external process. The external process would do its thing and then httppost some data back to the MVC application using the PostData() controller action.. but ultimately I need the data back in my MainMethod() thread since it is orchestrating many of these smaller external processes.

You need to do the processing of the posted results in the PostData action, not in MainMethod . That's the request that's being made when you want to do your work, so that's the action that you need to write the code to handle it.

If you are wanting your method to wait until the process is finish to then continue. Make the method asynchronous.

public async Task<string> PostData()
{
    //Put 'await' infront of the call
}

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