我正在结束教程,无法解决程序中的问题。当我走到我的路线时,我得到这样的内部服务器错误:
处理请求时发生未处理的异常。 InvalidOperationException:无法解析类型的服务 'Microsoft.AspNetCore.Identity.UserManager`1 [AspNetCoreTodo.Models.ApplicationUser]' 尝试激活时 'AspNetCoreTodo.Controllers.TodoController'。 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp,Type type,Type requiredBy,bool isDefaultParameterRequired)
InvalidOperationException:无法解析类型的服务 'Microsoft.AspNetCore.Identity.UserManager`1 [AspNetCoreTodo.Models.ApplicationUser]' 尝试激活时 “ AspNetCoreTodo.Controllers.TodoController”。
以此判断,我认为问题出在我的控制器上,具体来说就是我的索引方法。到目前为止,它一直有效,但是当我尝试为待办事项列表找到授权用户时出现此错误。基本上代码在这里,我认为错误可能在这一行:
var items = await _todoItemService.GetIncompleteItemsAsync(currentUser);
这是我的控制器代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using AspNetCoreTodo.Services;
using AspNetCoreTodo.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
namespace AspNetCoreTodo.Controllers
{
[Authorize]
public class TodoController : Controller
{
private readonly ITodoItemService _todoItemService;
private readonly UserManager<ApplicationUser> _userManager;
public TodoController(ITodoItemService todoItemService, UserManager<ApplicationUser> userManager)
{
_todoItemService = todoItemService;
_userManager = userManager;
}
public async Task<IActionResult> Index()
{
var currentUser = await _userManager.GetUserAsync(User);
//var currentUser = await _userManagerFindByIdAsync
if(currentUser == null)
{
return Challenge();
}
var items = await _todoItemService.GetIncompleteItemsAsync(currentUser);
var model = new TodoViewModel()
{
Items = items
};
return View(model);
}
Startup.cs //配置
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews();
services.AddRazorPages();
services.AddMvc();
services.AddAuthentication();
services.AddScoped<ITodoItemService, TodoItemService>();
}
我已解决此问题,但将其留给可能遇到类似问题的任何人。我要做的就是在startup.cs文件中用ApplicationUser替换IdentityUser并在_LoginPartial.cshtml中更改这2行:
@inject SignInManager IdentityUser SignInManager
@inject UserManager IdentityUser UserManager
与:
@inject SignInManager应用程序用户SignInManager
@inject UserManager ApplicationUser UserManager
此后,您应该在IdentityDbContex之后添加类型,以便在ApplicationDbContext.cs中拥有类似的内容,但前提是您收到上下文错误:
namespace AspNetCoreTodo.Data
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<TodoItem> Items {get; set;}
}
}
答案 0 :(得分:1)
此行:
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
我认为应该使用ApplicationUser
而不是IdentityUser
作为AddDefaultIdentity
的通用参数。