简体   繁体   中英

Passing data from a WebApi Controller to an MVC Controller

I have data coming in to a WebApi controller which gets converted into ac# object, and I need to pass this to an MVC controller, 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 between MVC controllers. Is there a method to pass data between an APIController and an MVC controller? (I'm not using a database).

Here is the action in my APIController which gets 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?

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