1

I don't know if my google skills are diminishing or what but I can't seem to figure out how to consume a local api. This may be best explained with sample code...

So I have a simple api

public class FooApiController : Controller
{
    public IActionResult GetFoo(int id)
    {
        if (id == 0)
            return BadRequest();

        var data = ... do db access
        return Ok(data);
    }
}

and a view controller

public class FooController : Controller
{
    public IActionResult Foo()
    {
        var api = new FooApiController();
        var data = api.GetFoo(1);

        ViewBag.Data = data;
        return View();
    }
}

So in the above view controller I call the api to get the data needed. However, being that the api controller returns an IActionResult, ViewBad.Data ends up being an IActionResult object. So how do I change the above to check the StatusCode of the api call, handle errors if need be, and if not... put just the data into the ViewBag, instead of the entire result object.

Every sample I have found seems to have the view controller return a view that then uses an ajax call to get the data. While I understand and could easily do that, I don't like the idea of making 2 round trips to the server when I don't need to.

2
  • I understand not wanting to do another round trip, but why not right a service that the api uses to make the db call and then use that service in the view controller to make the same db call. Commented Oct 17, 2016 at 3:20
  • @Woot I know it is probably cleaner, I guess it just seemed redundant. So we are talking about a service that returns the actual data objects, an api controller that returns a result object (holding the data), and a view controller that returns a view (with the data). It just seems like there is a more efficient way. Commented Oct 17, 2016 at 5:34

1 Answer 1

2

You are doing it wrong. If you want to reuse the code among multiple controllers, then it is better to move it from the GetFoo method and put it into a shared class and access it from everywhere else.

If you want to call it from a view through REST, then call it using $.ajax ex:

$.ajax('FooApi/GetFoo/5',function(data){alert(data);});

If you want to access it from another C# client, then use the HttpClient class, ex:

 HttpClient client = new HttpClient();
 client.BaseAddress = new Uri("http://localhost");
 client.DefaultRequestHeaders.Accept.Clear();
 client.DefaultRequestHeaders.Accept.Add(new    MediaTypeWithQualityHeaderValue("application/json"));
 HttpResponseMessage response = await client.PostAsJsonAsync("api/FooApi/GetFoo", 3);
 response.EnsureSuccessStatusCode();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.