I am new to web api. I have written a web api, based on vs 2022 mvc, to communicate with my sqlserver as :
using cd.Data;
using cd.ModelsCd;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace cd.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CdsController : ControllerBase
{
private readonly cdContext _context;
public CdsController(cdContext context)
{
_context = context;
}
// GET: api/Cds
[HttpGet]
public async Task<ActionResult<IEnumerable<Cd>>> GetCd()
{
return await _context.Cd.ToListAsync();
}
// GET: api/Cds/5
[HttpGet("{id}")]
public async Task<ActionResult<Cd>> GetCd(int id)
{
var cd = await _context.Cd.FindAsync(id);
if (cd == null)
{
return NotFound();
}
return cd;
}
}
the code i used to get info is :
static async Task LectureAsync2()
{
client.BaseAddress = new Uri("http://WebApiCd:7134/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
// get 1 CD
Console.WriteLine("GET 1 Cd");
fich Cds1 = null;
HttpResponseMessage response = await client.GetAsync("http://webapicd:7134/api/Cds/11");
if (response.IsSuccessStatusCode)
{
Cds1 = await response.Content.ReadAsAsync<fich>();
}
Console.WriteLine(Cds1.CdNom);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
I have tried to use it with vs 2022 in a Windows console app => ok, in Windows .net maui => ok, in vs 2022 .net maui android emulator or samsung s21 connetced to my PC, the result is Dotnet connection failure.
Network config is ok, because I have entered in the samsung web browser : http://webapicd:7134/api/Cds/11 and I have received a good response.
Can you, please, indicate what am i doing wrong in android ?
Thanks in advance.
eric pregnon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.