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?