I already have some Actions written in my Controllers but I'd need to use them as they were APIs.
So I wonder if it's possible to call a single Controller Action in an ApiController.
At the end I would like to have something like this:
public MyController : Controller
{
public ActionResult Index()
{
return View();
}
//or any more complex action
}
public MyApis : ApiController
{
// some code that returns the same
// MyController/Index/View result as an API URI
}
The main reason for this choice is that I'm working on a multi tier solution in VisualStudio.
And I have Controllers in a dedicated project, while I want to have ApiControllers in a different one.
Sure! So for your Action method pertaining to the view you would like to render you would write something basic like this.
Controllers without processes.
From here you can then begin to create your API controller methods as you have for your ActionResult methods the only difference are the return types. You api controller should correspond to whatever model instance your are intending to run queries against. Personally I prefer to create a model pertaining to each database abstraction ie. ContactController, AccountController and so on.
As you can see this is architect-ed from the Empty Api controller class. Lastly, you can call your api controller from whatever view you would like by running the following Jquery code.
As for your post method...
The javascript here came from a knockout.js method if you need a full code snippet to see how to wrap this all together with knockout let me know! But I hope this will get you moving in the right direction!