如何将视图重定向到主页 - ASP.NET Core MVC
How to redirect view to homepage - ASP.NET Core MVC
这是我的第一个 ASP.NET 核心 MVC 项目,也是我的第一个 C# 体验。我正在创建一个模仿 ubereats/doordash.
的网络应用程序
现在,我正在编写 register/login 系统并将其与 SQL 数据库合并。 register/create 帐户功能运行良好,但现在我需要弄清楚如何在某人登录时对其进行身份验证。
所以,我写算法的方式是:
- 用户创建帐户 - 数据库已更新(运行良好)。
- 用户点击登录超链接 - 将他们带到登录页面(同样有效)
- 用户输入电子邮件和密码并按下提交(我在这里没有收到任何错误,但我认为我没有正确创建 form/authenticated)。
- 控制器获取用户输入的电子邮件和密码 - 如果数据库找到电子邮件,则检查密码是否正确。
- 如果正确 - 将用户重定向到主页,否则刷新并再次显示登录表单。
我需要帮助来检查和检索数据库中的项目,然后验证它们是否正确。
如果登录成功,我还需要帮助将用户重定向到主页(位于视图中的“主页”文件夹内)。 C# 和 asp.net 似乎非常先进,所以我想知道是否有一个我不知道的简单函数可能有用。
有人可以帮助我让登录部分正常工作吗?
登录控制器的这个特定部分是我认为的问题:
// GET: Logins/Create
public IActionResult LoginPortal()
{
return View();
}
[HttpPost]
public async Task<IActionResult> LoginPortal(int id)
{
var movie = await _context.Login.FindAsync(id);
if (movie == null)
{
return View();
}
return View("Home/Index");
}
这是我目前的情况:
LoginsController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; //including necessary directories
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using EasyEats.Data;
using EasyEats.Models;
namespace EasyEats.Controllers
{
public class LoginsController : Controller //log-in & register controller
{
private readonly EasyEatsContext _context;
public LoginsController(EasyEatsContext context)
{
_context = context;
}
// GET: Logins
public async Task<IActionResult> Index()
{
return View(await _context.Login.ToListAsync());
}
// GET: Logins/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var login = await _context.Login
.FirstOrDefaultAsync(m => m.ID == id);
if (login == null)
{
return NotFound();
}
return View(login);
}
// GET: Logins/Create
public IActionResult Create()
{
return View();
}
// POST: Logins/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,FirstName,LastName,Address,Email,PhoneNumber,Password")] Login login)
{
if (ModelState.IsValid)
{
_context.Add(login);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(login);
}
// GET: Logins/Create
public IActionResult LoginPortal()
{
return View();
}
[HttpPost]
public async Task<IActionResult> LoginPortal(int id)
{
var movie = await _context.Login.FindAsync(id);
if (movie == null)
{
return View();
}
return View("Home/Index");
}
// POST: Logins/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,FirstName,LastName,Address,Email,PhoneNumber,Password")] Login login)
{
if (id != login.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(login);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!LoginExists(login.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(login);
}
// GET: Logins/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var login = await _context.Login
.FirstOrDefaultAsync(m => m.ID == id);
if (login == null)
{
return NotFound();
}
return View(login);
}
// POST: Logins/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var login = await _context.Login.FindAsync(id);
_context.Login.Remove(login);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool LoginExists(int id)
{
return _context.Login.Any(e => e.ID == id);
}
}
}
Login.cs(型号)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EasyEats.Models
{
public class Login //class for customer log-in & registration information
{
public int ID { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
public string Address { get; set; }
[Required]
public string Email { get; set; }
[Required]
[Display(Name = "Phone Number")]
public int PhoneNumber { get; set; }
[Required]
public string Password { get; set; }
}
}
在我的“Views”文件夹中:我有一个 Home 文件夹、Logins 文件夹和 Shared 文件夹
Index.cshtml(在 Views 的“Home”文件夹内)
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
LoginPortal.cshtml(在视图的“登录”文件夹内)
@model EasyEats.Models.Login
@{
ViewData["Title"] = "Create";
}
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="LoginPortal">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password" class="control-label"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Log in" class="btn btn-primary" />
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
_Layout.cshtml(共享文件夹内)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - EasyEats</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">EasyEats</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<partial name="_LoginPartial" />
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
© 2020 - EasyEats - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@RenderSection("Scripts", required: false)
</body>
</html>
_LoginPartial.cshtml(共享文件夹内)
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Logins"
asp-action="Create">Register</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Logins"
asp-action="LoginPortal">Login</a>
</li>
</ul>
以下是一些图片:
Homepage
Create Account page
Login Page
I need help going through and retrieving items in the database and
then verifying if they are correct.
您可以在数据库中找到该模型,其电子邮件与您从 form.Then 发布的电子邮件相同,请检查您找到的模型的密码。
I also need help redirecting the user to the homepage (which is inside of the "Home" folder in views) if the login is successful.
你可以使用 return RedirectToAction("Index", "Home");
.
这是一个工作演示:
控制器:
public IActionResult LoginPortal()
{
return View();
}
[HttpPost]
public async Task<IActionResult> LoginPortal(Login login)
{
//for easy testing,i just hardcode the list
var list = new List<Login>() {
new Login(){Email="a",Password="123"},
new Login(){Email="b",Password="111"}
};
//you could find it in the database
//var movie = _context.Login.Where(a => a.Email == login.Email).FirstOrDefault();
var movie = list.Where(a => a.Email == login.Email).FirstOrDefault();
if (movie == null)
{
return View(login);
}
if (movie.Password==login.Password)
{
return RedirectToAction("Index", "Home");
}
return View();
}
结果:
更新:
您可以像下面这样设置 ModelState 错误:
[HttpPost]
public async Task<IActionResult> LoginPortal(Login login)
{
//...
if (movie == null)
{
ModelState.AddModelError("Email", "Do not find such email");
return View(login);
}
if (movie.Password==login.Password)
{
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError(string.Empty, "invalid login attempt");
return View();
}
这是我的第一个 ASP.NET 核心 MVC 项目,也是我的第一个 C# 体验。我正在创建一个模仿 ubereats/doordash.
的网络应用程序现在,我正在编写 register/login 系统并将其与 SQL 数据库合并。 register/create 帐户功能运行良好,但现在我需要弄清楚如何在某人登录时对其进行身份验证。
所以,我写算法的方式是:
- 用户创建帐户 - 数据库已更新(运行良好)。
- 用户点击登录超链接 - 将他们带到登录页面(同样有效)
- 用户输入电子邮件和密码并按下提交(我在这里没有收到任何错误,但我认为我没有正确创建 form/authenticated)。
- 控制器获取用户输入的电子邮件和密码 - 如果数据库找到电子邮件,则检查密码是否正确。
- 如果正确 - 将用户重定向到主页,否则刷新并再次显示登录表单。
我需要帮助来检查和检索数据库中的项目,然后验证它们是否正确。
如果登录成功,我还需要帮助将用户重定向到主页(位于视图中的“主页”文件夹内)。 C# 和 asp.net 似乎非常先进,所以我想知道是否有一个我不知道的简单函数可能有用。
有人可以帮助我让登录部分正常工作吗?
登录控制器的这个特定部分是我认为的问题:
// GET: Logins/Create
public IActionResult LoginPortal()
{
return View();
}
[HttpPost]
public async Task<IActionResult> LoginPortal(int id)
{
var movie = await _context.Login.FindAsync(id);
if (movie == null)
{
return View();
}
return View("Home/Index");
}
这是我目前的情况:
LoginsController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; //including necessary directories
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using EasyEats.Data;
using EasyEats.Models;
namespace EasyEats.Controllers
{
public class LoginsController : Controller //log-in & register controller
{
private readonly EasyEatsContext _context;
public LoginsController(EasyEatsContext context)
{
_context = context;
}
// GET: Logins
public async Task<IActionResult> Index()
{
return View(await _context.Login.ToListAsync());
}
// GET: Logins/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var login = await _context.Login
.FirstOrDefaultAsync(m => m.ID == id);
if (login == null)
{
return NotFound();
}
return View(login);
}
// GET: Logins/Create
public IActionResult Create()
{
return View();
}
// POST: Logins/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID,FirstName,LastName,Address,Email,PhoneNumber,Password")] Login login)
{
if (ModelState.IsValid)
{
_context.Add(login);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(login);
}
// GET: Logins/Create
public IActionResult LoginPortal()
{
return View();
}
[HttpPost]
public async Task<IActionResult> LoginPortal(int id)
{
var movie = await _context.Login.FindAsync(id);
if (movie == null)
{
return View();
}
return View("Home/Index");
}
// POST: Logins/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,FirstName,LastName,Address,Email,PhoneNumber,Password")] Login login)
{
if (id != login.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(login);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!LoginExists(login.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(login);
}
// GET: Logins/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var login = await _context.Login
.FirstOrDefaultAsync(m => m.ID == id);
if (login == null)
{
return NotFound();
}
return View(login);
}
// POST: Logins/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var login = await _context.Login.FindAsync(id);
_context.Login.Remove(login);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool LoginExists(int id)
{
return _context.Login.Any(e => e.ID == id);
}
}
}
Login.cs(型号)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EasyEats.Models
{
public class Login //class for customer log-in & registration information
{
public int ID { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
public string Address { get; set; }
[Required]
public string Email { get; set; }
[Required]
[Display(Name = "Phone Number")]
public int PhoneNumber { get; set; }
[Required]
public string Password { get; set; }
}
}
在我的“Views”文件夹中:我有一个 Home 文件夹、Logins 文件夹和 Shared 文件夹
Index.cshtml(在 Views 的“Home”文件夹内)
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
LoginPortal.cshtml(在视图的“登录”文件夹内)
@model EasyEats.Models.Login
@{
ViewData["Title"] = "Create";
}
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="LoginPortal">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password" class="control-label"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Log in" class="btn btn-primary" />
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
_Layout.cshtml(共享文件夹内)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - EasyEats</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">EasyEats</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<partial name="_LoginPartial" />
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
© 2020 - EasyEats - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@RenderSection("Scripts", required: false)
</body>
</html>
_LoginPartial.cshtml(共享文件夹内)
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Logins"
asp-action="Create">Register</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-controller="Logins"
asp-action="LoginPortal">Login</a>
</li>
</ul>
以下是一些图片: Homepage
Create Account page Login Page
I need help going through and retrieving items in the database and then verifying if they are correct.
您可以在数据库中找到该模型,其电子邮件与您从 form.Then 发布的电子邮件相同,请检查您找到的模型的密码。
I also need help redirecting the user to the homepage (which is inside of the "Home" folder in views) if the login is successful.
你可以使用 return RedirectToAction("Index", "Home");
.
这是一个工作演示:
控制器:
public IActionResult LoginPortal()
{
return View();
}
[HttpPost]
public async Task<IActionResult> LoginPortal(Login login)
{
//for easy testing,i just hardcode the list
var list = new List<Login>() {
new Login(){Email="a",Password="123"},
new Login(){Email="b",Password="111"}
};
//you could find it in the database
//var movie = _context.Login.Where(a => a.Email == login.Email).FirstOrDefault();
var movie = list.Where(a => a.Email == login.Email).FirstOrDefault();
if (movie == null)
{
return View(login);
}
if (movie.Password==login.Password)
{
return RedirectToAction("Index", "Home");
}
return View();
}
结果:
更新:
您可以像下面这样设置 ModelState 错误:
[HttpPost]
public async Task<IActionResult> LoginPortal(Login login)
{
//...
if (movie == null)
{
ModelState.AddModelError("Email", "Do not find such email");
return View(login);
}
if (movie.Password==login.Password)
{
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError(string.Empty, "invalid login attempt");
return View();
}