C# ASP.NET Core与EF Core中的注册页出错
我正在使用Gill Cleeren制作的Visual Studio 2017 Pluralsight课程构建您的第一个ASP.NET Core 2.1 MVC应用程序 我已经打开了最后一章,其中包含完整的Bethany's Pie Shop应用程序 在下一步中,我构建了解决方案,在PackageManager控制台中键入update database并运行该应用程序 应用程序正在运行,直到我尝试注册用户名为止 我不知道到底是什么错误,但是数据没有在AspNetUsers表中注册 您可以在下面找到AccountController.cs文件:C# ASP.NET Core与EF Core中的注册页出错,c#,asp.net,asp.net-mvc,asp.net-core,entity-framework-core,C#,Asp.net,Asp.net Mvc,Asp.net Core,Entity Framework Core,我正在使用Gill Cleeren制作的Visual Studio 2017 Pluralsight课程构建您的第一个ASP.NET Core 2.1 MVC应用程序 我已经打开了最后一章,其中包含完整的Bethany's Pie Shop应用程序 在下一步中,我构建了解决方案,在PackageManager控制台中键入update database并运行该应用程序 应用程序正在运行,直到我尝试注册用户名为止 我不知道到底是什么错误,但是数据没有在AspNetUsers表中注册 您可以在下面找到
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Identity;
using BethanysPieShop.ViewModels;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace BethanysPieShop.Controllers
{
public class AccountController : Controller
{
private readonly SignInManager<IdentityUser> _signInManager;
private readonly UserManager<IdentityUser> _userManager;
public AccountController(SignInManager<IdentityUser> signInManager, UserManager<IdentityUser> userManager)
{
_signInManager = signInManager;
_userManager = userManager;
}
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel loginViewModel)
{
if (!ModelState.IsValid)
return View(loginViewModel);
var user = await _userManager.FindByNameAsync(loginViewModel.UserName);
if (user != null)
{
var result = await _signInManager.PasswordSignInAsync(user, loginViewModel.Password, false, false);
if (result.Succeeded)
{
return RedirectToAction("Index", "Home");
}
}
ModelState.AddModelError("", "User name/password not found");
return View(loginViewModel);
}
public IActionResult Register()
{
return View(new LoginViewModel());
}
[HttpPost]
public async Task<IActionResult> Register(LoginViewModel loginViewModel)
{
if (ModelState.IsValid)
{
var user = new IdentityUser() { UserName = loginViewModel.UserName };
var result = await _userManager.CreateAsync(user, loginViewModel.Password);
if (result.Succeeded)
{
return RedirectToAction("Index", "Home");
}
}
return View(loginViewModel);
}
[HttpPost]
public async Task<IActionResult> Logout()
{
await _signInManager.SignOutAsync();
return RedirectToAction("Index", "Home");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using BethanysPieShop.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Identity;
namespace BethanysPieShop
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<AppDbContext>();
//services.AddTransient<IPieRepository, MockPieRepository>();
services.AddTransient<IPieRepository, PieRepository>();
services.AddTransient<IFeedbackRepository, FeedbackRepository>();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseStatusCodePages();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Identity;
使用BethanysPieShop.ViewModels;
//有关为空项目启用MVC的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=397860
名称空间bethanspieshop.Controllers
{
公共类AccountController:控制器
{
专用只读签名管理器\u签名管理器;
私有只读用户管理器_UserManager;
公共帐户控制器(SignInManager SignInManager、UserManager UserManager)
{
_signInManager=signInManager;
_userManager=userManager;
}
公共IActionResult登录()
{
返回视图();
}
[HttpPost]
公共异步任务登录(LoginViewModel LoginViewModel)
{
如果(!ModelState.IsValid)
返回视图(loginViewModel);
var user=await\u userManager.FindByNameAsync(loginViewModel.UserName);
如果(用户!=null)
{
var result=await _signInManager.PasswordSignInAsync(user,loginViewModel.Password,false,false);
if(result.successed)
{
返回重定向到操作(“索引”、“主页”);
}
}
AddModelError(“,“未找到用户名/密码”);
返回视图(loginViewModel);
}
公共IActionResult寄存器()
{
返回视图(新的LoginViewModel());
}
[HttpPost]
公共异步任务寄存器(LoginViewModel LoginViewModel)
{
if(ModelState.IsValid)
{
var user=new IdentityUser(){UserName=loginViewModel.UserName};
var result=await\u userManager.CreateAsync(用户,loginViewModel.Password);
if(result.successed)
{
返回重定向到操作(“索引”、“主页”);
}
}
返回视图(loginViewModel);
}
[HttpPost]
公共异步任务注销()
{
等待_signInManager.SignOutAsync();
返回重定向到操作(“索引”、“主页”);
}
}
}
请告诉我您是否需要其他文件
你知道我如何解决这个问题吗
谢谢
LE:似乎我可以从数据库中检索信息。因此,我认为问题可能来自代码。我还将在下面发布Startup.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Identity;
using BethanysPieShop.ViewModels;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace BethanysPieShop.Controllers
{
public class AccountController : Controller
{
private readonly SignInManager<IdentityUser> _signInManager;
private readonly UserManager<IdentityUser> _userManager;
public AccountController(SignInManager<IdentityUser> signInManager, UserManager<IdentityUser> userManager)
{
_signInManager = signInManager;
_userManager = userManager;
}
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel loginViewModel)
{
if (!ModelState.IsValid)
return View(loginViewModel);
var user = await _userManager.FindByNameAsync(loginViewModel.UserName);
if (user != null)
{
var result = await _signInManager.PasswordSignInAsync(user, loginViewModel.Password, false, false);
if (result.Succeeded)
{
return RedirectToAction("Index", "Home");
}
}
ModelState.AddModelError("", "User name/password not found");
return View(loginViewModel);
}
public IActionResult Register()
{
return View(new LoginViewModel());
}
[HttpPost]
public async Task<IActionResult> Register(LoginViewModel loginViewModel)
{
if (ModelState.IsValid)
{
var user = new IdentityUser() { UserName = loginViewModel.UserName };
var result = await _userManager.CreateAsync(user, loginViewModel.Password);
if (result.Succeeded)
{
return RedirectToAction("Index", "Home");
}
}
return View(loginViewModel);
}
[HttpPost]
public async Task<IActionResult> Logout()
{
await _signInManager.SignOutAsync();
return RedirectToAction("Index", "Home");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using BethanysPieShop.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Identity;
namespace BethanysPieShop
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<AppDbContext>();
//services.AddTransient<IPieRepository, MockPieRepository>();
services.AddTransient<IPieRepository, PieRepository>();
services.AddTransient<IFeedbackRepository, FeedbackRepository>();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseStatusCodePages();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.DependencyInjection;
使用BethanysPieShop.Models;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.Extensions.Configuration;
使用Microsoft.AspNetCore.Identity;
名称空间BethanySpeeshop
{
公营创业
{
公共IConfiguration配置{get;}
公共启动(IConfiguration配置)
{
配置=配置;
}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
services.AddDbContext包括堆栈跟踪。如果没有错误,但未修改AspNetUsers表,则很有可能您的连接字符串指向与您正在查看的数据库不同的数据库。单击register后,我会在If(ModelState.IsValid)行上获得一个断点在Register方法中。您可以通过访问屏幕截图的以下链接看到。Kirk,当我更新数据库时,表被创建,并且它还包含一些表上的数据。我已经验证并注意到连接字符串指向本地数据库。LinkedListT,您能告诉我如何包含堆栈跟踪吗?Than谢谢。