[Solved] Postman using AngularJS Error 404 Not Found in ASP.Net MVC

Last Reply one month ago By dharmendr

Posted 2 months ago

Hi.

I am programming a CRUD with Angular, C #, SqlServer and MVC5, but the POST part sends me an error 404 Not Found status and I can't find what it is.

Please, could someone review the code and tell me that it is generating the error that does not allow Include a new car.

I will be attenative to your answers.

Cheers

CONTROLLER
app.controller("miCrudController", function ($scope, CrudService) {
    $scope.divVehiculos = false;
    listarVehiculos();
    function listarVehiculos() {
        var vehiculoData = CrudService.consultarVehiculos();
        vehiculoData.then(function (carro) {        
            $scope.carros = carro.data;
        }, function() {
            toastr["error"]("No se pudo recuperar el listado", "Mensaje de Error");
        });
    }

    $scope.obtenerPorId = function (coche) {
        var vehiculoData = CrudService.consultarVehiculosPorId(coche.Id);

        vehiculoData.then(function (_coche) {

            $scope.carro = _coche.data;
            $scope.vehiculoId = coche.Id;
            $scope.Marca = coche.Marca;
            $scope.Modelo = coche.Modelo;
            $scope.Color = coche.Color;
            $scope.Anio = coche.Anio;

            $scope.Accion = "Modificar";
            $scope.divVehiculos = true;

        }, function () {
            toastr["error"]("No se pudo obtener el vehículo por su código", "Mensaje de Error");

        });
    }    
    $scope.AgregarModificarVehiculo = function () {
        var miCoche = {

            Marca: $scope.Marca,
            Modelo: $scope.Modelo,
            Color: $scope.Color,
            Anio: $scope.Anio
        };
        var valorAccion = $scope.Accion
        
        if (valorAccion == "Modificar") {
            alert("Entrando1");
            miCoche.Id = $scope.vehiculoId;
            var vehiculoData = CrudService.modificarVehiculos(miCoche);

            vehiculoData.then(function (datos) {
                listarVehiculos();
                $scope.divVehiculos = false;
                if (datos.status == 200) {
                    toastr["success"]("Registro modificado con éxito", "Información");
                    
                }
            }, function () {
                toastr["error"]("No se pudo modificar el vehículo", "Mensaje de Error");
            });
        } else {
            alert("Entrando2");
            var vehiculoData = CrudService.incluirVehiculos(miCoche);

            vehiculoData.then(function (datos) {
                listarVehiculos();
                if (datos.status == 200) {
                    toastr["success"]("Registro incluido con éxito", "hola");
                }
            }, function () {
                toastr["error"]("No se pudo incluir el vehículo", "Mensaje de Error");
            });
        }
    }

    $scope.ingresarVehiculo = function () {
        limpiarCampos();
        $scope.Accion = "Incluir";
        $scope.divVehiculos = true;
    }   

    $scope.deleteVehiculo = function(coche) {

        var vehiculoData = CrudService.eliminarVehiculos(coche.Id);
        vehiculoData.then(function (datos) {

            if (datos.status == 200) {
                toastr["success"]("Registro eliminado con éxito", "Mensaje de Error");
            }
            listarVehiculos();

        }, function () {
            toastr["error"]("No se pudo el Vehículo", "Mensaje de Error");
        });
    }

    $scope.Cancelar = function () {
        $scope.divVehiculos = false;
    }

    function limpiarCampos() {
        $scope.Marca = "";
        $scope.Modelo = "";
        $scope.Color = "";
        $scope.Anio = "";
    }
});

SERVICE

app.service("CrudService", function ($http) {
    //Recuperar Todos los Vehiculos.
    this.consultarVehiculos = function () {
        return $http.get("/api/v1/public/getvehiculo");
    }

    //Recuperar Vehiculos Por Id.
    this.consultarVehiculosPorId = function (id) {
        return $http.get("/api/v1/public/getvehiculo/" + id);
    }

    //Modificar Vehiculos
    this.modificarVehiculos = function (coche) {
        var response = $http({
            method: "put",
            url: "/api/v1/public/putvehiculo",
            data: JSON.stringify(coche),
            dataType: "json"
        });
        return response;
    }
         //Incluir(Add) Vehiculos
        this.incluirVehiculos = function (coche) {
            var response = $http({
                method: "post",
                url: "/api/v1/public/postvehiculo",
                data: JSON.stringify(coche),
                dataType: "json"
            });
            return response;
    }

    //Eliminar Vehiculos
        this.eliminarVehiculos = function (id) {
            var response = $http({
                method: "delete",
                url: "/api/v1/public/deletevehiculo/" + JSON.stringify(id)
               });
            return response;
        }

});

VEHICULOS CONTROLLER

namespace EjemploCrudMVC5AngJs.Controllers
{
    [RoutePrefix("api/v1/public")]
    public class VehiculosController : ApiController
    {
        private readonly VehiculosDbContext db = new VehiculosDbContext();

        [HttpGet]
        [Route("getvehiculo")]
        public IQueryable<Vehiculos> recuPerarVehiculos()
        {
            return db.Coches;
        }

        [HttpGet]
        [Route("getvehiculo/{id:int}")]
        public HttpResponseMessage recuperarPorId(int id)
        {
            if (id <= 0)
                return Request.CreateResponse(HttpStatusCode.BadRequest);

            Vehiculos varcoche = db.Coches.Find(id);
            return Request.CreateResponse(HttpStatusCode.OK, varcoche);
        }

        [HttpPut]
        [Route("putvehiculo")]
        public HttpResponseMessage modificar(Vehiculos coche)
        {
            if (coche == null)
                return Request.CreateResponse(HttpStatusCode.BadRequest);

            db.Entry(coche).State = EntityState.Modified;
            db.SaveChanges();

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        [HttpPost]
        [Route("postvehiculo")]
        HttpResponseMessage Incluir(Vehiculos coche)
        {
            if (coche == null)
                return Request.CreateResponse(HttpStatusCode.BadRequest);

            db.Coches.Add(coche);
            db.SaveChanges();

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        
        [HttpDelete]
        [Route("deletevehiculo/{id:int}")]
        public HttpResponseMessage Eliminar(int id)
        {
            if (id <= 0)
                return Request.CreateResponse(HttpStatusCode.BadRequest);

            Vehiculos varcoche = db.Coches.Find(id);
            db.Coches.Remove(varcoche);
            db.SaveChanges();
            return Request.CreateResponse(HttpStatusCode.OK);
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);

        }
    }
}