Error with Asp.NET Web.API + AngularJS

4

I have my method C# so

[ResponseType(typeof(Categoria))]
public async Task<IHttpActionResult> Post(Categoria model) {
    if (!ModelState.IsValid) {
        return BadRequest(ModelState);
    }
    if (_repositorio.InsertOrUpdate(model, out Res)) {
        _repositorio.Save();
        return CreatedAtRoute("DefaultApi", new { id = model.Id }, model);
    }
    return BadRequest("Erro ao tentar salvar, tente novamente mais tarde");    
}

I have my factory like this:

.factory("categoriasService", function($http, config, $q) {
    var _postItem = function(record) {
        var deferred = $q.defer();
        $http.post(config.baseUrl + "/api/Categoria/Post", record).then(
            function(result) {
                deferred.resolve(result.data);
            },
            function (erroResult) {
                deferred.reject();
            }
        );
        return deferred.promise;
    };
    return {            
        postItem: _postItem
    };
})

When calling in my controller :

.controller("newCategoriaCtrl", [
    "$scope", "$http", "$window", "categoriasService", "modalConfirmationService", "$routeParams",
    function ($scope, $http, $window, categoriasService, modalConfirmationService, $routeParams) {            
        if ($routeParams.id !== undefined) {
            categoriasService.find($routeParams.id).then(function (result) {
                $scope.newCategoria = result;
            });
            $scope.titleAcao = "Alterar";
        } else {
            $scope.newCategoria = { Id: 0 };
            $scope.titleAcao = "Cadastrar";
        }

        $scope.save = function() {
            //console.log($scope.newCategoria);
            categoriasService.postItem($scope.newCategoria)
            .then(function (newCategoria) {
                console.log(newCategoria);//aqui o objeto está sempre vazio por que acontece o erro 500.
                modalConfirmationService.getModalInstance("Sucesso", "Dados salvos com sucesso!");
            },
            function() {
                modalConfirmationService.getModalInstance("Erro", "Não foi possível executar sua ação, tente novamente mais tarde.");
            })
            .then(function() {
                $window.location = "#";
            });
        };
    }
]);

When I call the application, without activating the debug in the browser console, error 500 always occurs and does not save the information in the database.

http://localhost:54100/api/Categoria/Post 500 (Internal Server Error)

But if I activate debug mode in VS, it gives the same error in the console, but the information is saved in the database.

What's wrong? Do I have to configure something in the web.api app?

    
asked by anonymous 18.06.2015 / 03:09

1 answer

0

Only with the above information is it difficult to help, but the 500 error happens, because you do not have a specific treatment to return the error to the screen, that is. in the DEBUG version of VS. your controller (webApi) is being called. however probably time to run this line of code

return CreatedAtRoute("DefaultApi", new { id = model.Id }, model); should be bursting an error, so the error that returns to the client is 500 As soon as there is no treatment, what I usually use is an error handling

Try {} catch () {}. where I return an object of HttpResponseMessage with the status treated by me, and an exception to receive on the client and perform the treatments.

    
12.08.2015 / 00:06