首先,我在控制器api中有一个方法来向数据库添加记录。低于我的方法:
[HttpPost]
public Response<UserView> AddUser(UserView user)
{
EntityFirstApp.User mappedUser = Mapper.Map <EntityFirstApp.User>(user);
var entityUser = this._userService.AddUser(mappedUser);
UserView userView = Mapper.Map<UserView>(entityUser);
//// build response
var response = new Response<UserView> { Model = userView };
response.Messages.Add(new Message
{
MessageType = MessageType.Success,
Value = "Success"
});
return response;
}
在添加另一种删除记录的方法之前,该方法很有效。低于我的其他方法
[HttpPost]
public Response<UserView> DeleteUser(Int64 Id)
{
var entityUser = this._userService.GetUserById(Id);
this._userService.DeleteUser(Id);
UserView userView = Mapper.Map<UserView>(entityUser);
//// build response
var response = new Response<UserView> { Model = userView };
response.Messages.Add(new Message
{
MessageType = MessageType.Success,
Value = "Success"
});
return response;
}
添加 DeleteUser 方法后,我再也无法使用AddUser方法了。我得到无法加载资源:服务器在浏览器控制台中响应状态为400(错误请求)。在前面我使用angularjs来使用该方法。低于我前面的代码。
userModule.service('userService', userService);
userService.$inject = ['$http', '$q', 'commonEnum'];
function userService($http, $q, commonEnum) {
var userService = {
getAllUser: getAllUser,
addUser: addUser,
deleteUser : deleteUser
}
return userService;
function getAllUser() {
var deferred = $q.defer();
$http({
method: commonEnum.ajaxMethod.get,
url: commonEnum.baseApiUrl.base + commonEnum.webApiUrl.getAllUser,
})
.success(onSuccess)
.error(onError);
function onSuccess(data) {
deferred.resolve(data);
}
function onError() {
deferred.reject();
}
return deferred.promise;
}
function addUser(user) {
var deferred = $q.defer();
$http({
method: commonEnum.ajaxMethod.post,
url: commonEnum.baseApiUrl.base + commonEnum.webApiUrl.addUser,
data: user
})
.success(onSuccess)
.error(onError);
function onSuccess(data) {
deferred.resolve(data);
}
function onError() {
deferred.reject();
}
return deferred.promise;
}
function deleteUser(userId) {
var parameters = {
Id: userId
}
var deferred = $q.defer();
$http({
method: commonEnum.ajaxMethod.post,
url: commonEnum.baseApiUrl.base + commonEnum.webApiUrl.deleteUser,
params: parameters
})
.success(onSuccess)
.error(onError);
function onSuccess(data) {
deferred.resolve(data);
}
function onError() {
deferred.reject();
}
return deferred.promise;
}
}
我仍然无法弄明白为什么。任何帮助将不胜感激。感谢。