Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create ASP.Core WebAPI with Xamarin.Forms

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

like image 918
SantaZ Avatar asked Oct 28 '22 01:10

SantaZ


2 Answers

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);
  }
  ...
}
like image 136
Athanasios Kataras Avatar answered Nov 15 '22 06:11

Athanasios Kataras


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:

  • Right click on the solution in the Solution Explorer
  • Pick Properties
  • At the left side of the properties page, select Startup Projects
  • Multiple startup projects radio box
  • Action column set Start on every project you want to debug
  • Then ok and play

Hope this helped.

Useful links

  • Create an ASP App From the official documentation
  • Debug multiple projects answered by Max .
like image 29
Fabrice T Avatar answered Nov 15 '22 06:11

Fabrice T