I want to launch a web api host when launching the application on xamarin, which I will make requests for, but I don’t understand how to connect the web api project and xamarin.forms or is there some other way of debugging the application.
I know that you can deploy the application on the azure service, but I want it to work locally
Those are two different applications.
Xamarin.Forms
Xamarin.Forms is an open-source UI framework. Xamarin.Forms allows developers to build Android, iOS, and Windows applications from a single shared codebase.
WebApi Core
ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the . NET Framework.
The first one is deployed on app stores and installed on devices.
The web api is deployed in web servers (like IIS) or on azure, and can be accessed over internet via http calls.
During development, you can build and deploy on your localhost. Check here for setup guidance: https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio
You can consume your api from xamarin forms as described here: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest
Example:
public async Task<List<TodoItem>> RefreshDataAsync ()
{
...
var uri = new Uri (string.Format (Constants.TodoItemsUrl, string.Empty));
...
var response = await _client.GetAsync (uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync ();
Items = JsonConvert.DeserializeObject <List<TodoItem>> (content);
}
...
}
This is pretty simple with Visual Studio 2019/2017.
After creating your Xamarin forms project, add an ASP.NET Core app to your solution; Visual Studio will manage everything. To debug the ASP project, set it as Startup project and click on the beautify Play/Start button and the browser should start and wait for a breakpoint or your stuff ...
You can debug Xamarin and the ASP Web app at the same time.
First we assume that the Web App act as an API endpoint for your app ; so that you will call it to send or request data ... Then we assume you have a web client to call your endpoint and controllers in your Web app to receive that requests and respond ...
OK all that given, do this:
Hope this helped.
Useful links
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With