【问题标题】:Can't delete user through Http post request in ASP.NET Identity 3.0无法通过 ASP.NET Identity 3.0 中的 Http 发布请求删除用户
【发布时间】:2020-08-23 19:09:29
【问题描述】:

这是我的代码:

    [HttpPost("{id}")]
    [Route("DeleteUserProfile")]
    public async Task<IActionResult> DeleteUserProfile(string id) 
    {
        var user = await _userManager.FindByIdAsync(id);
        _app.Users.Remove(user);
        await _app.SaveChangesAsync();
        return RedirectToAction(nameof(Index));

    }

_app 是我的上下文。我删除 AspNetUsers 的方法对吗?当我在 Postman 中发布请求时,我得到 404 not found。请帮忙!我的网址是:http://localhost:57392/api/UserProfile/DeleteUserProfile/292207c9-e961-4073-b9b8-260e86f7cbe0。

【问题讨论】:

  • 尝试将HttpPost 属性更改为HttpPost("DeleteUserProfile/{id}"),而不是Route。此外,当DELETE 完全适合已完成的操作时,您可能不应该使用POST。这也意味着您不一定需要路径中的DeleteUserProfile。
  • 您是否为“api/UserProfile”配置了RoutePrefix?
  • 404,我做到了。我没有找到与提供的值匹配的路线。我必须检查那个错误,但我不知道为什么它不起作用,因为 getallusers() 方法像魅力一样工作......
  • 它有效,我只需要更改 RedirectToAction() 中的参数,我的问题是路由,应该把 [HttpDelete("DeleteUserProfile/{id}")] 没有路由部分。谢谢大家: D

标签: c# asp.net .net asp.net-web-api


【解决方案1】:

我不应该放 Route,正确的 http 请求是:

[HttpDelete("DeleteUserProfile/{id}")]
public async Task<IActionResult> DeleteUserProfile(string id) 
    {
        var user = await _app.Users.FindAsync(id);
        if (user == null) 
        {
            return NotFound();
        }
        _app.Users.Remove(user);
        await _app.SaveChangesAsync();
        return RedirectToAction("GetAllUsers","UserProfile",);

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 2019-11-14
    • 2012-01-09
    • 1970-01-01
    相关资源
    最近更新 更多