Take the 2-minute tour ×

I'm working on an MVC4 project that has site controllers and API controllers. How do I send a request to an API controller from one of the site controllers?

For example, I might have an APiController that looks like this:

public class FooController : ApiController {
    public int Add(int a, int b) {
        return a + b;

How would I send a request to /FooController/Add?a=1;b=2 from this Controller?

public class BarController : Controller {
    public int AddOneAndTwo() {
        //What goes here?

EDIT: What I really wanted to do was call methods from my ApiController class from my site Controller, and I assumed I had to conjure some MVC4 magic to make it work. As per the comments between Felix and me, I can just create an instance of my ApiController in my site Controller and go from there. But if you still need to access ApiController methods via HTTP, then boy howdy, is Felix's answer to my original question for you.

share|improve this question
if you want to send a request through http you might use restsharp.org, however inside the same project why don't you just call the action method? –  Felix Sep 25 '12 at 22:09
How would I call the action method? I thought I was going to have to send an HTTP request because my site Controller isn't inherently aware of any ApiController instances. (I'm new to MVC4 =P) –  Kevin Sep 25 '12 at 22:18
Sorry, I might be unclear, if you have your web api controller in the same project as your BarController, you can access it like any other class in your project. Of cause in this case any of MVC handlers, attributes and filters would not be called... pardon for my english –  Felix Sep 25 '12 at 22:42
Ah, that makes sense. I'll probably take that route then. Thank you for your help! =) –  Kevin Sep 26 '12 at 7:05

1 Answer 1

up vote 2 down vote accepted

The code with restsharp would be something like this:

    protected IRestResponse GetResponse(int a, int b)
        var client = new RestClient
            BaseUrl = "http://localhost:8888/api/FooController"
        var request = new RestRequest
            DateFormat = DataFormat.Xml.ToString(),
            Resource = "Add",
            Method = Method.GET
            JsonSerializer.JsonSerialize(new {a, b}),
        return client.Execute(request);
share|improve this answer

Your Answer


