简体   繁体   中英

Call MVC Controller Method from web API

Hi I am trying to update client side of my application from webAPI. So when someone calls web api this invokes method that push data to controller or to javascript to show the data to user viewing View.

I tried calling methods and even trying to call POST to controller but it never get to controllers method. I have simple method in controller like this:

[HttpPost]
public ActionResult Refresh(string id)
{
    if (User.Identity.Name == id) return RedirectToAction("Index");
    else return null;
}

I don't know if it is possible to refresh client like this, i think better will be some JS what i will implement. But I didn't find answer anywhere. Everyone is calling webApi from Controller but not the other way. How can I accomplish that?

It needs to define the route whereas the action within parameter will be recognized.

Ex: [Route("api/refresh/{id}")]

There is a reason why everyone calls an api from an mvc controller.

The purpose of an API is to provide data or do some data manipulation to whatever has the necessary permissions to request such actions. That's it.

An API does not care if it's called by an MVC application, or a mobile app or a JAVA app or a JavaScript front end or who knows what else.

Of course, it is perfectly ok to call another api from your main api for whatever reason.

Now, you must have a very good reason why you'd want to call an MVC controller from a WebApi which is basically the reverse of any normal paradigm.

Let's assume you have a valid reason. MVC actions (controller methods ) can return views or data, just like a normal api. You can return Views or you can return Json data for example. In fact MVC actions can behave like WebApi endpoints which means they can get called the same way. You just need to make sure that you get your URLs right, so WebApi calls your MVC controller via it's properly qualified route.

To test this, write a simple MVC action which returns some Json data and call it in the browser. If you manage to craft the URL correctly you will see the data displayed in the browser. Now use the same URL and issue a similar request from a WebApi controller using the full URL like you did in the browser.

Assuming you have something like this in MVC:

public JsonResult DemoCall(int number)
{
     return Json($"{number}", JsonRequestBehavior.AllowGet);
}

This basically mimics a get web api call, I placed this method in the HomeController. You could run the website, let's say it runs on port 55712, then you can use something like like this in the browser to see the result:

http://localhost:55712/home/democall?number=3

All you have to do now, from your web api is to issue a get request ( using HttpClient for example) to this url and you will get the data. Then you can decide what you want to do with it. The same applies for POST methods.

Of course the really big question is why would you need to call an MVC action from a WebApi, but if you really need to do it then it's quite simple. Treat it just like any other api.

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