将字符串参数id与控制器参数匹配

时间:2017-12-18 20:35:10

标签: c# asp.net-core

我正在开发一个ASP.NET Core 2.0项目,我遇到了与此相匹配的问题:

<a asp-controller="User" asp-action="AddFriend" asp-route-id="@item.Id">Add 
Friend</a>

到UserController中的方法:

[ValidateAntiForgeryToken]
public IActionResult AddFriend(string id)
{
    if (id == null)
    {
        return NotFound();
    }

    User currentUser =  _manager.GetUserAsync(HttpContext.User).GetAwaiter().GetResult();
    User targetUser = _service.GetById(id);

    _service.AddFriend(currentUser, targetUser);

    return RedirectToAction(actionName: nameof(Index), controllerName: "Home");
}

0 个答案:

没有答案