I have data coming in to a WebApi controller which gets converted into a c# object, and I need to pass this to an MVC conroller, at the moment I am holding this object in a static "Globals" class and then assigning an instance of my ViewModel to that object in my MVC controller which then returns as usual. I'm aware of using TempData to pass data beteewn MVC controllers. Is there a method to between an apiController and an MVC controller? (I'm not using a database).
Here is the action in my ApiController which gets sent the message by an external device:
[Route("someroute"), HttpPost]
public IHttpActionResult Post([FromBody] object obj)
{
try
{
MessageViewModel model = new MessageViewModel();
model.Object = obj;
Globals.model = model;
return Ok();
}
catch
{
return BadRequest();
}
}
I need to pass that model to my MVC controller. At the moment I am storing it in a static class and then my MVC controller:
public ActionResult Index()
{
return View(Globals.model);
}
Is returning it when a client calls the action.
@peco is absolutely correct, you need to move logic outside mvc controller and reuse it in web api. If for some reason you cant do this, what preventing you from redirect with necessary parameters?