Click here to Skip to main content
15,887,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello every one i working a project with angular and asp .net core web api

i already have the DB so i did the scaffolding and it generate the models but when i create the controller with entity framwork with actions and i want to test the api i get this :
get : http://localhost:28102/api/Responsables

the result
this web site can't be reached .

i don t know what s the problem .

What I have tried:

My contoller :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ApiBack.Models;

namespace ApiBack.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ResponsablesController : ControllerBase
    {
        private readonly gestion_livraisonContext _context;

        public ResponsablesController(gestion_livraisonContext context)
        {
            _context = context;
        }

        // GET: api/Responsables
        [HttpGet]
        public IEnumerable<Responsable> GetResponsable()
        {
            return _context.Responsable;
        }

        // GET: api/Responsables/5
        [HttpGet("{id}")]
        public async Task<IActionResult> GetResponsable([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var responsable = await _context.Responsable.FindAsync(id);

            if (responsable == null)
            {
                return NotFound();
            }

            return Ok(responsable);
        }

        // PUT: api/Responsables/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutResponsable([FromRoute] int id, [FromBody] Responsable responsable)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != responsable.IdResponsable)
            {
                return BadRequest();
            }

            _context.Entry(responsable).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ResponsableExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/Responsables
        [HttpPost]
        public async Task<IActionResult> PostResponsable([FromBody] Responsable responsable)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _context.Responsable.Add(responsable);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetResponsable", new { id = responsable.IdResponsable }, responsable);
        }

        // DELETE: api/Responsables/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteResponsable([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var responsable = await _context.Responsable.FindAsync(id);
            if (responsable == null)
            {
                return NotFound();
            }

            _context.Responsable.Remove(responsable);
            await _context.SaveChangesAsync();

            return Ok(responsable);
        }

        private bool ResponsableExists(int id)
        {
            return _context.Responsable.Any(e => e.IdResponsable == id);
        }
    }
}





My Model :

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace ApiBack.Models
{
    public partial class Responsable
    {
        public Responsable()
        {
            Agence = new HashSet<Agence>();
        }
        [Key]
        public int IdResponsable { get; set; }
        public string NomResponsable { get; set; }
        public string PrenomResponsable { get; set; }
        public string LoginResponsable { get; set; }
        public string MdpResponsable { get; set; }
        public int? Ville { get; set; }

        public Ville VilleNavigation { get; set; }
        public ICollection<Agence> Agence { get; set; }
    }
}





the dbcontext class:


using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace ApiBack.Models
{
    public partial class gestion_livraisonContext : DbContext
    {
       

        public gestion_livraisonContext(DbContextOptions options)
            : base(options)
        {
        }

        public virtual DbSet<Agence> Agence { get; set; }
        public virtual DbSet<ClientPar> ClientPar { get; set; }
        public virtual DbSet<ClientPro> ClientPro { get; set; }
        public virtual DbSet<Commande> Commande { get; set; }
        public virtual DbSet<Livreur> Livreur { get; set; }
        public virtual DbSet<Responsable> Responsable { get; set; }
        public virtual DbSet<Superadmin> Superadmin { get; set; }
        public virtual DbSet<Ville> Ville { get; set; }
    }
}
Posted
Comments
Richard MacCutchan 15-Jun-20 10:25am    
I am not a web expert but I did fail to notice ...
Your URL is http://localhost:28102/api/Responsables. But every reference in your code above spells it Responsable, without the trailing s.
Richard Deeming 15-Jun-20 14:11pm    
Except for the ResponsablesController, which is what should define the route. :)
Richard MacCutchan 15-Jun-20 15:35pm    
I did say I'm not an expert.
Member 13926859 16-Jun-20 6:45am    
how should i define the route i m new in asp core and i don t know how to do it
Richard Deeming 16-Jun-20 7:05am    
The route looks OK to me. Are you sure the port number's correct?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900