0

So at this point, I have two projects in my VS solution:

RecipeAPI, which is a ASP.Net Core Application RecipeUI, which is a Windows Forms Application

Inside the RecipeAPI project, I already have the model and controller classes made and now I have to create a prototype UI in which the functionality should be implemented.

My issue is using the classes and objects from the API project, into the UI one. For example:

I am trying to populate a ListBox. Inside RecipeAPI, I have a controller class named RecipeController, which has the following method for retrieving data from the database:

        [Route("v1/recipe/{recipeBy}")]
    [HttpGet()]
    public List<Recipe> GetRecipes(string recipeBy)
    {
        using (var con = _connFactory())
        {
            con.Open();
            return con.Query<Recipe>("SELECT * FROM dbo.Recipe WHERE RecipeBy = @recipeBy", new { recipeBy }).ToList();
        }
    }

This was tested using PostMan, works perfectly fine. However, I am having huge issues trying to figure out how to populate the ListBox. In fact, I am having issues figuring out how I can use certain methods from a project to another. To be noted that I have already added references.

Moving forward, this is the method in the Forms class which is supposed to populate the ListBox.

    private void ShowRecipes(string recipeBy)
    {
        List<Recipe> recipeList = new List<Recipe>;
        recipeList = GetRecipes(recipeBy);
        recipeListBox.DataSource = recipeList;
    }

I am not entirely sure how correct this is. Basically what I am trying to do is get the list of recipes by calling the method GetRecipes() from RecipeController. Unfortunately, I am getting errors such as:

The type or namespace name 'Recipe' could not be found The name 'GetRecipes' does not exist in the current context

So what am I missing here? Am I even on the right track or I shouldn't even try combining Web Forms and ASP.Core? And if this works too, then how can I be able to access methods from RecipeAPI.Controllers in RecipeUI Form?

DerStarkeBaer
  • 669
  • 8
  • 28
Questieme
  • 913
  • 2
  • 15
  • 34
  • 1
    You'll need to make http requests (e.g. `HttpClient`) to your API in order to use it, after all, that's what APIs are for. – Haytam Sep 03 '19 at 08:35
  • 1
    You need an [HttpClient](https://learn.microsoft.com/it-it/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2) object to send requests to an API controller: – Davide Vitali Sep 03 '19 at 08:36
  • It looks like you are using Entity to connect to the database. Receipe/GetReceipes are interfaces from the c# application to the classes entity created that maps to the database. The RecipeAPI is the Web application which is connected to the database. So the Form project (the client) must send a Request (HttpClient or equivalent) to get the data from the Web application (the server) – jdweng Sep 03 '19 at 09:00
  • Thank you for your input! Highly appreciated. – Questieme Sep 03 '19 at 09:17

1 Answers1

0

You have to make Api Call to the RecipeAPI project from Windows Forms Application

in order to use the methods/logic of the APIs Project.

This is the way to make an API call: How to make HTTP POST web request

Muhammad Aftab
  • 1,098
  • 8
  • 19