[英]Creating a Login Page in ASP.NET Core MVC
我正在嘗試創建一個登錄頁面。 但它說沒有找到頁面。
我已將其包含在Startup.cs
:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{Controller=LoginController}/{action=Login}");
});
該Login.cshtml
頁面是意見Views/Login/Login.cshtml
的控制器下和控制器所在的名稱文件夾LoginController.cs
的代碼LoginController.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AdminControl.Controllers
{
public class LoginController : Controller
{
// POST: LoginController
[HttpPost]
public IActionResult Login()
{
return View();
}
}
}
Login.cshtml
:
<form asp-action="Users/Index" class="form-group" id="login-form" method="post" runat="server" style="text-align: center;">
<input class="form-control" placeholder="ID" runat="server" style="text-align: center;"/>
<input class="form-control" placeholder="Password" runat="server" style="text-align: center;" type="password"/>
</form>
<button class="btn btn-info" form="login-form" value="Submit" type="submit">Submit</button>
出了什么問題?
修復模式中的錯誤:“{Controller=LoginController}/{action=Login}”);
pattern: "{Controller=Login}/{action=Login}");
但最好使用標准的默認模式
pattern: "{controller=Home}/{action=Index}/{id?}");
並在檢查用戶是否尚未登錄后從索引操作重定向到登錄控制器
正如@IsmailDiari 所注意到的,您應該有兩個操作 - 一個用於獲取登錄表單,另一個用於發布登錄表單。 但你還需要一個模型:
public class LoginViewModel
{
public string Login {get; set;}
public string Password {get; set;}
}
在此更改控制器和視圖后,如下所示:
[HttpGet]
public IActionResult Login()
{
var viewModel=new LoginViewModel;
return View(viewModel);
}
// POST: When submitting the login credentials
[HttpPost]
public IActionResult Login(LoginViewModel viewModel)
{
}
登錄表格:
@model LoginViewModel
@using (Html.BeginForm())
{
<input class="form-control" asp-for = "Login" name="login" placeholder="Login" runat="server" style
="text-align: center;"/>
<input class="form-control" asp-for="Password" name="password" placeholder="Password" runat="server" style="text-align: center;" type="password"/>
<input type="submit" value="Login" class="btn btn-primary" />
}
請參閱此鏈接以了解如何設置默認路由,您做錯了。 您還需要提供Get操作以返回視圖。 您的控制器應如下所示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AdminControl.Controllers
{
public class LoginController : Controller
{
// Get: Return Login View
[HttpGet]
public IActionResult Login()
{
return View();
}
// POST: When submitting the login credentials
[HttpPost]
public IActionResult Login(FormCollection collection)
{
//1. validation Goes here
//2. redirection after validation,
//return View();
}
}
}
你的 cshtml 正文應該是這樣的
<body>
@using (Html.BeginForm())
{
<input class="form-control" name="id" placeholder="ID" runat="server" style
="text-align: center;"/>
<input class="form-control" name="password" placeholder="Password" runat="server" style="text-align: center;" type="password"/>
<input type="submit" value="Login" class="btn btn-primary" />
}
</body>
您應該將name屬性設置為您的輸入,以便您稍后可以在 FormCollection 中找到它們
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.