0

swagger ui

I want to connect my first maui test app with ASP.NET Web API that retrieves data from mongoDB. When the ASP.NET Web API is running, I can easily retrieve my data from Swagger UI, but when it comes to retrieving data in my MAUI app, I get an error

Connection Failure

Could you explain me why is this happening and what I should do about it?

Here is my code from MAUI app:

private async void OnSubmitClicked(object sender, EventArgs e)
{
    string pesel = peselEntry.Text;

    try
    {
        var response = await App.HttpClient.GetAsync($"https://localhost:7183/api/Patients/Test");
        if (response.IsSuccessStatusCode)
        {
            var patient = await response.Content.ReadFromJsonAsync<Patients>();
            await DisplayAlert("Oto dane", $"Imię: {patient.Name}, Nazwisko: {patient.Surname}", "OK");
        }
        else
        {
            await DisplayAlert("Błąd", "Pacjent nie znaleziony.", "OK");
        }
    }
    catch (Exception ex)
    {
        await DisplayAlert("Błąd", $"Wystąpił problem: {ex.Message}", "OK");
    }
}

I just added CORS to program.cs in ASP.NET, but it didn't help

10
  • 1
    use the IP or FQDN of your server, not localhost Commented Oct 23, 2024 at 14:59
  • Doesn't work either Commented Oct 23, 2024 at 15:03
  • Then you have a network issue. Is your local server setup to handle remote requests? Commented Oct 23, 2024 at 15:12
  • I honestly don't know. What I do know is that when I created the Windows Forms app, it pulled data from an API that connected to the postgresql database without any issues. How can I configure these requests? Commented Oct 23, 2024 at 15:18
  • it depends on what server you're using, but its well documented. You can test using the browser on your mobile device (or emulator) to confirm that it can connect to the swagger endpoint Commented Oct 23, 2024 at 15:20

1 Answer 1

0

A WinForms app runs on your local machine, so can access a localhost site. A Maui app runs (usually) in an emulator, which is a different machine.

As IISExpress can only serve to localhost, you need to setup your site to run under IIS. You can still debug from there! The Maui app then needs to call that site by machine name or IP address.

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.