简体   繁体   中英

.net How to Call a webapi action from MVC Controller

Which is the best way of calling a web api action method from a mvc controller?

I know I could invoke the methods directly, as if there were ordinary services, but this approach would not call the configurated filters (among other things) that are configured with the web api controller/action.

Besides, as I want to do it in a generical approach, i would have to do it using reflection, and this way, I would have to implement the same logic used in webapi for action parameter matching, which could be very tricky to mimic.

I´ve seen this post: How can i call a WebApi method from MVC Controller Action Method?

Is this the correct answer? I wonder whether isn´t there a more cleaner way,like the RedirectToAction method would in a MVC--> MVC controller redirection.

Thanks

I decided to try the suggested solution but got stuck on another issue:

My API Controller action is returning an interface, which has 3 implementations.

I could not choose which implementation to use when deserializing the Json, when invoking the method that way.

I gave up doing this way, and resigned myself to make an extra browser call for now.

I´m closing this question, as I think I should ask a new one for the issue I just described.

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