Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用标识从Web Api 2中的数据库中删除用户_C#_Asp.net Web Api2_Asp.net Identity - Fatal编程技术网

C# 使用标识从Web Api 2中的数据库中删除用户

C# 使用标识从Web Api 2中的数据库中删除用户,c#,asp.net-web-api2,asp.net-identity,C#,Asp.net Web Api2,Asp.net Identity,标题上写着什么。我试过几种方法,但都不管用。如果有人有时间,我会非常感谢你的帮助 我的Ajax请求: $('#deleteUserBtn').click(function () { $.ajax({ url: 'Profile/DeleteUser', method: 'DELETE', headers: { 'Authorization': 'Bearer ' + sessionStorage.g

标题上写着什么。我试过几种方法,但都不管用。如果有人有时间,我会非常感谢你的帮助

我的Ajax请求:

$('#deleteUserBtn').click(function () {

    $.ajax({
        url: 'Profile/DeleteUser',
        method: 'DELETE',
        headers: {
        'Authorization': 'Bearer '
            + sessionStorage.getItem("accessToken")
        },
        success: function (data) {
            sessionStorage.removeItem('accessToken');
            window.location.href = "../Login.html";
        },
        error: function (jQXHR) {
        }
    });
});
控制器不工作,但它会让你知道我在做什么

控制员:

[RoutePrefix("Personal_Project/Main_Page_Personal_Project")]
[Route("Profile/DeleteUser")]
[HttpDelete]
[Authorize]
public void DeleteUser()
{

   string userId = User.Identity.GetUserId();
   ApplicationUser LoggedUser = db.Users.Find(userId);

   db.Users.Remove(LoggedUser);
   db.SaveChanges();

}
我在浏览控制台中遇到此错误:

jquery-3.3.1.min.js:2删除500(内部服务器错误)

我访问了这些链接,但能找到答案。有人帮忙吗


您可以将附加条目的状态设置为“已删除”:

string userId = _User.Identity.GetUserId();
ApplicationUser loggedUser = db.Users.Find(userId);
db.Entry(loggedUser ).State = EntityState.Deleted;
db.SaveChanges();
多亏了“dotnetdev”,我学会了使用try-catch块。我的内部异常建议我还需要删除与Users表中的外键连接的另一个表。这就是我最后要做的:

public void DeleteUser()
{
     string userId = User.Identity.GetUserId();
     ApplicationUser LoggedUser = db.Users.Find(userId);

     db.Users.Remove(LoggedUser);

     AdditionalInfo info = db.AdditionalInfo.Find(userId); // Added this
     db.AdditionalInfo.Remove(info); // Added this

     db.SaveChanges();

}

我可以看到你的url不完整。你可以在你的c#代码周围添加try-catch块并看到异常。路由正常!刚刚添加了“RoutePrefix”。我尝试返回同一路线上的号码,没有问题。将条目状态附加到“已删除”没有帮助。无论如何,谢谢你抽出时间。