[英]Issue deleting ASP.NET Identity in MVC Web Application
So after looking around for guides and tutorials of how can I delete ASP Users, I found the following code to be pretty neat:因此,在查找有关如何删除 ASP 用户的指南和教程后,我发现以下代码非常简洁:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(string id)
{
if (ModelState.IsValid)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var user = await UserManager.FindByIdAsync(id);
var logins = user.Logins;
var rolesForUser = await UserManager.GetRolesAsync(id);
using (var transaction = context.Database.BeginTransaction())
{
foreach (var login in logins.ToList())
{
await UserManager.RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
}
if (rolesForUser.Count() > 0)
{
foreach (var item in rolesForUser.ToList())
{
// item should be the name of the role
var result = await UserManager.RemoveFromRoleAsync(user.Id, item);
}
}
await UserManager.DeleteAsync(user);
transaction.Commit();
}
return RedirectToAction("Index");
}
else
{
return View();
}
}
My view looks something like this:我的观点是这样的:
<td>
@Html.ActionLink("Edit", "Edit", new { id = user.UserId }) |
@Html.ActionLink("Delete", "DeleteConfirmed", new { id = user.UserId })
</td>
After clicking "Delete" here, in theory, it should have called the DeleteConfirmed method from the controller called "ManageUsersController".在此处单击“删除”后,理论上它应该从名为“ManageUsersController”的控制器调用了 DeleteConfirmed 方法。 However, it returns this error:
但是,它返回此错误:
Server Error in '/' Application.
“/”应用程序中的服务器错误。
The resource cannot be found.
无法找到该资源。
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.
描述:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、更改名称或暂时不可用。 Please review the following URL and make sure that it is spelled correctly.
请检查以下 URL 并确保其拼写正确。
Requested URL: /ManageUsers/DeleteConfirmed/29ad177f-0285-43d2-b065-109876f270b9
请求的 URL:/ManageUsers/DeleteConfirmed/29ad177f-0285-43d2-b065-109876f270b9
What might be going wrong here?这里可能出了什么问题? Is there another way that I should write the method in the controller?
还有另一种方法可以在控制器中编写方法吗? Thank you in advance
先感谢您
This answer is based on the default codes that .NET scaffold for us.这个答案基于 .NET 为我们搭建的默认代码。
Delete
link using an extension of ActionLink
which needs link text as the first parameter and action name as the second one .ActionLink
的扩展生成Delete
链接,该扩展需要链接文本作为第一个参数,操作名称作为第二个参数。 Your DeleteConfirmed
action is a POST
method;DeleteConfirmed
操作是一个POST
方法; you can't generate a link to POST, .NET sees GET methods for links.<td>
@Html.ActionLink("Edit", "Edit", new { id = user.UserId }) |
@Html.ActionLink("Delete", "Delete", new { id = user.UserId })
</td>
Make sure you have another method called Delete
which is a GET
one, in your controller.确保您的控制器中有另一个名为
Delete
方法,它是一个GET
方法。
Add another attribute to DeleteConfirmed
:向
DeleteConfirmed
添加另一个属性:
[ActionName("Delete")]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DeleteConfirmed(string id)
{ ... }
Delete
, you should use a POST form including the user id and anti-forgery token as hidden inputs and a submit button instead of link.Delete
时需要删除用户,则应使用包含用户 ID 和防伪令牌作为隐藏输入和提交按钮而不是链接的 POST 表单。 But it's a best practice to show the user what they're deleting.Delete
(to review) and DeleteConfirmed
(to actually delete). Delete
(审查)和DeleteConfirmed
(实际删除)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.