I have very little experience with the API. I am trying to transfer data about viewing a product record from my mobile app .NET MAUI to the server ASP.NET using an HttpPost request. I am trying to transfer data from the mobile client in the form of JSON using String Content, as suggested in the documentation. But I still couldn’t find information on how to get this JSON and convert it to an instance of the View class.
My API HttpPost code:
[Route("api/[controller]")]
[ApiController]
public class ViewController : ControllerBase
{
// POST api/<ViewController>
[HttpPost]
public async void Post(int id, DateOnly date, TimeOnly time)
{
using (VapeshopContext db = new VapeshopContext())
{
View view = new View
{
Date = date,
Time = time,
ProductId = id
};
db.Views.Add(view);
await db.SaveChangesAsync();
}
}
}
My POST request code:
private async void toInfoPage(object obj)
{
if (obj != null)
{
var p = obj as Product;
using StringContent jsonContent = new(
JsonSerializer.Serialize(new
{
Date = DateOnly.FromDateTime(DateTime.Now),
Time = TimeOnly.FromDateTime(DateTime.Now),
ProductId = p.Id
}),
Encoding.UTF8, "application/json");
await httpClient.PostAsJsonAsync("http://10.0.2.2:5125/api/View",jsonContent);
var newPage = new ProductInfo(obj as Product);
await Navigation.PushAsync(newPage);
}
}