我在我的网络API中创建方法是为了能够删除用户,但是我用于其他控制器的方法是否有效?我不接受405 - 方法。
PostController(工作):
public IHttpActionResult DeletePost(int id)
{
Post post = controller.Post(id);
if (post == null)
{
return NotFound();
}
controller.DeletePost(id);
return Ok(post);
}
UserController(不工作):
public IHttpActionResult DeleteUser(string username)
{
User user = controller.User(username);
if (user == null)
{
return NotFound();
}
controller.DeleteUser(username);
return Ok(user);
}
这是UserController的Delete方法的DAL:
public void DeleteUser(string username)
{
SqlConnection con = new SqlConnection("Server=localhost;Database=X;User Id=X;Password=X;");
string sql = "DELETE FROM [User] WHERE Username=@Username";
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@Username", username);
cmd.ExecuteNonQuery();
}
注意:该查询直接在SQL Management Studio中工作
编辑角部分(注意:它们位于同一控制器和模板中):
$scope.delete = function (id) {
Post.delete({ id: id }, function () {
$scope.getPosts();
}
);
};
$scope.deleteUser = function (id) {
User.delete({ id: id }, function () {
console.log("delete button triggered");
$scope.getUsers();
}
);
};