我正试图通过让登录用户键入一个数字来更新数据库,并使用登录管理器和用户管理器从数据库中添加该数字。我希望它获取存储在数据库中的登录用户的帐户余额,并将其添加到他们键入的数字中。然后我想用组合号码更新数据库。
然而,我一直纠结于如何让控制器使用登录管理器。有了剃须刀片,我所要做的就是使用@inject SignInManager<CardUs> SignInManager
和使用@inject UserManager<CardUs> UserManager
并导入Microsoft.AspNetCore.Identity
但当我尝试使用UserManager.GetUserAsync(User).Result.AccountBalance
时,它会抛出很多错误。
其中之一是:
C#8.0中没有"顶级语句"功能。请使用语言版本9.0或更高版本的
所以我从8.0更新到9.0,但后来没用。现在它说
不能在此上下文中使用在顶级语句中声明的本地变量或本地函数"SigninManager">
我试着查看Microsoft页面,但一无所获。这是错误代码CS8801。
如何在控制器中使用签名管理器和用户管理器变量?
此项目尚未完成。我还没有把它发送到数据库。它所做的一切都需要用户输入并在下一个屏幕上显示
代码:
会计主管:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Card.Areas.Identity.Data;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Card.Models;
SignInManager<CardUs> SignInManager;
UserManager<CardUs> UserManager;
namespace Card.Controllers
{
[Authorize]
public class AccountController : Controller
{
private readonly TransactionConnection _tc;
public AccountController(TransactionConnection tc)
{
_tc = tc;
}
public IActionResult Index()
{
return View();
}
public IActionResult Transactions()
{
var results = _tc.TransactionHistory.ToList();
return View(results);
}
[HttpGet]
public IActionResult Deposit()
{
return View();
}
[HttpPost]
public IActionResult Deposit(DepositModel model)
{
int test = model.AccountBalance + UserManager.GetUserAsync(User).Result.AccountBalance;
return Content($"this is how much you entered {test}");
}
public IActionResult Transfers()
{
return View();
}
}
}
Deposit.cshtml:
@model Card.Models.DepositModel
@{
ViewData["Title"] = "Deposit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<form asp-action="Deposit" asp-controller="Account">
<label asp-for="AccountBalance"></label>
<input asp-for="AccountBalance" placeholder="How much do you want"/>
<input type="submit"/>
</form>
DepositModel:
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Card.Areas.Identity.Data;
namespace Card.Models
{
public class DepositModel
{
public int AccountBalance { get; set; }
}
}
以下是登录经理在剃须刀页面上的样子:
@using Microsoft.AspNetCore.Identity
@using Card.Areas.Identity.Data
@inject SignInManager<CardUs> SignInManager
@inject UserManager<CardUs> UserManager
@*
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
<style>
div.a {
text-align: right;
}
h1 {
text-align: right;
}
.left {
text-align: left;
}
.right {
text-align: right;
}
.left, .right {
display: inline-block;
width: 50%;
}
</style>
<div class="alert alert-primary" role="alert">
<div>
<div class="left"><p1>Hello <strong>@UserManager.GetUserAsync(User).Result.FirstName</strong> welcome back!</p1></div><div class="right">account balance: $@UserManager.GetUserAsync(User).Result.AccountBalance </div>
</div>
</div>
我想明白了。我把它设置错了。我必须将它添加到构造函数中,并且必须创建和分配字段。
代码:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Card.Areas.Identity.Data;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Card.Models;
namespace Card.Controllers
{
[Authorize]
public class AccountController : Controller
{
private readonly TransactionConnection _tc;
private readonly UserManager<CardUs> userManager;
private readonly SignInManager<CardUs> signInManager;
public AccountController(TransactionConnection tc, UserManager<CardUs> userManager, SignInManager<CardUs> signInManager)
{
_tc = tc;
this.userManager = userManager;
this.signInManager = signInManager;
}
public IActionResult Index()
{
return View();
}
public IActionResult Transactions()
{
var results = _tc.TransactionHistory.ToList();
return View(results);
}
[HttpGet]
public IActionResult Deposit()
{
return View();
}
[HttpPost]
public IActionResult Deposit(DepositModel model)
{
int test = model.AccountBalance + userManager.GetUserAsync(User).Result.AccountBalance;
return Content($"this is how much you entered {test}");
}
public IActionResult Transfers()
{
return View();
}
}
}
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Card.Areas.Identity.Data;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Card.Models;
namespace Card.Controllers {
SignInManager < CardUs > SignInManager;
UserManager < CardUs > UserManager;
[Authorize]
public class AccountController: Controller {
private readonly TransactionConnection _tc;
public AccountController(TransactionConnection tc) {
_tc = tc;
}
public IActionResult Index() {
return View();
}
public IActionResult Transactions() {
var results = _tc.TransactionHistory.ToList();
return View(results);
}
[HttpGet]
public IActionResult Deposit() {
return View();
}
[HttpPost]
public IActionResult Deposit(DepositModel model) {
int test = model.AccountBalance + SignInManager.GetUserAsync(User).Result.AccountBalance;
return Content($"this is how much you entered {test}");
}
public IActionResult Transfers() {
return View();
}
}
}